主要要注意的是,, 對實數高斯消元,注意判斷奇怪的情況,比如-0.00。。輸出的時候加個eps就好。。wa了好幾發
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"q"<<endl;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int,int>,int> PIII;
typedef pair<int,int> PII;
const double eps=1e-10;
const int MX=111;
const int P=313;
int n,L;
struct Matrix
{
double w[MX][MX];
int n;
void init(int x)
{
n=x;
for(int i=0; i<n; i++)
for(int j=0; j<=n; j++)w[i][j]=0;
}
};
struct AC_trie
{
int root,cnt;
int End[500+10],to[500+10][6],fail[500+10];//改
int newnode()
{
for(int i=0; i<6; i++) to[cnt][i]=-1;//改
End[cnt]=0;
return cnt++;
}
void init()
{
cnt=0;
root=newnode();
}
int insert(int *s)
{
//cout<<cnt<<endl;
int now=root;
for(int i=0; i<L; i++)
{
int v=s[i];//改
if(to[now][v]==-1)to[now][v]=newnode();
now=to[now][v];
}
End[now]=1;//改
return now;
}
void build()
{
queue<int> my;
fail[root]=root;
for(int i = 0; i < 6; i++)//改
if(to[root][i] == -1)
to[root][i]= root;
else
{
fail[to[root][i]]=root;
my.push(to[root][i]);
}
while(!my.empty())
{
int u=my.front();
my.pop();
//End[u]|=End[fail[u]];
for(int i=0; i<6; i++) //改
{
int v=to[u][i];
if(v!=-1)
{
fail[v]=to[fail[u]][i];
my.push(v);
}
else to[u][i]=to[fail[u]][i];
}
}
}
Matrix build_matrix()
{
Matrix ret;
ret.init(cnt);
ret.w[0][cnt]=-1;
for(int i=0; i<cnt; i++)
{
ret.w[i][i]=-1;
if(End[i]==0)
{
for(int j=0; j<6; j++)
//if(to[i][j]!=0)
ret.w[to[i][j]][i]+=1.0/6;
}
}
return ret;
}
} AC;
void print(Matrix a)
{
for(int i=0; i<a.n; i++)
for(int j=0; j<=a.n; j++)
printf("%.3f%c",a.w[i][j],j==a.n?'\n':' ');
}
int tmp[111];
int ans[MX];
int gauss(double arr[][MX],int n)
{
for(int i=0; i<n; i++)
{
int t=i;
for(int j=i+1; j<n; j++) if(fabs(arr[j][i])>fabs(arr[t][i])) t=j;
if(t!=i) for(int j=i; j<=n; j++) swap(arr[i][j],arr[t][j]);
for(int j=i+1; j<n; j++) if(fabs(arr[j][i])>eps)
{
double w=arr[j][i]/arr[i][i];
for(int k=i; k<=n; k++) arr[j][k]-=arr[i][k]*w;
}
}
for(int i=n-1; i>=0; i--)
{
arr[i][n]/=arr[i][i];
for(int j=i-1; j>=0; j--) arr[j][n]-=arr[j][i]*arr[i][n];
}
return 1;//唯一解
}
int main()
{
FIN;
freopen("output1.txt","w",stdout);
int _;
scanf("%d",&_);
while(_--)
{
scanf("%d%d",&n,&L);
AC.init();
for(int i=1; i<=n; i++)
{
for(int j=0; j<L; j++)
{
scanf("%d",&tmp[j]);
tmp[j]--;
}
ans[i]=AC.insert(tmp);
}
AC.build();
Matrix ret=AC.build_matrix();
//cout<<AC.cnt<<endl;
// print(ret);
gauss(ret.w,ret.n);
for(int i=1; i<=n; i++)
printf("%.6f%c",ret.w[ans[i]][ret.n]+eps,i==n?'\n':' ');
//scanf("%s",s);
}
return 0;
}