您的位置:首页 > 其它

UVALive 3490 Generator(AC自动机+dp+高斯消元)

2015-02-24 23:58 543 查看
题意就是,随机字母组成一个串,有一个目标串,当这个由随机字母组成的串出现目标串就停止,求这个随机字母组成串的期望长度。看了题解才知道的做法。首先肯定要把目标串弄到ac自动机里,当然kmp也一样因为就一个串,然后dp[i]表示从i这个节点走到末尾的期望长度,为何不是头走到i?因为失配指针的原因,因为可以知道这个节点如果失配下一个节点是什么,而要知道有几个节点失配后是这个节点则与前者是相反的。后者需要额外的操作,比如每个节点弄个vector,然后遍历一下ac自动机。但是这样完全没必要,我们修改一下递推就方向就完全避免了这个问题,这个做法在其他一些题中也有体现。

容易看出,dp[i] = 所有他下一步可能的节点dp[j]之和/m+1。可以想一下从头走到i的递推式,是dp[i] = 所有走到i这个节点的dp[j]+1。

然后就可以列出n个方程,dp[最后一个节点] = 0。然后用高斯消元求解。double会有误差,所以用了整数的,做法很简单,把方程式子左右乘个m,然后消元的时候比如第一行是3,第二行那个位置系数是2,那么对于后面的系数x1(第一行),x2(第二行),x2*3-x1*2即可。不过我有点疑惑的是为何方程的解一定是个整数呢?如果谁有见解可以告诉我。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-8
#define NMAX 201000
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
const int maxnode = 15;
struct Matrix
{
ll m[15][15];
};
Matrix x;
struct AhoCorasick
{
int ch[maxnode][26];
int val[maxnode],f[maxnode];
//    int last[maxnode];
int sz;
void clear()
{
memset(ch[0],0,sizeof(ch[0]));
sz = 1;
}
int idx(char c)
{
return c-'A';
}
void insert(char *s)
{
int u = 0, n = strlen(s);
for(int i= 0; i < n; i++)
{
int c = idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = 1;
}
void getFail()
{
queue<int>q;
f[0] = 0;
for(int c = 0; c < 26; c++)
{
int u = ch[0][c];
if(u){f[u] = 0; q.push(u);
//            last[u] = 0;
}
}
while(!q.empty())
{
int r = q.front(); q.pop();
for(int c = 0; c < 26; c++)
{
int u = ch[r][c];
if(!u)
{
ch[r][c] = ch[f[r]][c];
continue;
}
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
//                last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
}
void matrixmaker(int num,int n)
{
memset(x.m,0,sizeof(x.m));
for(int i = 0; i < n-1; i++)
{
x.m[i]
-= num;
x.m[i][i] -= num;
for(int j = 0; j < num; j++)
x.m[i][ch[i][j]]++;
}
x.m[n-1][n-1] = 1;
x.m[n-1]
= 0;
//        for(int i = 0; i < 4; i++)
//            for(int j = 0; j < 5; j++)
//            {
//                printf("%I64d%c",x.m[i][j],(j==4)?'\n':' ');
////                cout<<x.m[i][j]<<" "<<j<<endl;
////                cout<<"qu:"<<
//            }
}
}ac;
void gauss(int n)
{
for(int i = 0; i < n; i++)
{
int r = i;
while(r < n && !x.m[r][i]) r++;
if(r != i) for(int j = 0; j <= n; j++) swap(x.m[i][j],x.m[r][j]);
ll tmp = x.m[i][i];
for(int j = i+1; j < n; j++) if(x.m[j][i])
{
ll ha = x.m[j][i];
for(int k = i+1; k <= n; k++)
x.m[j][k] = x.m[j][k]*tmp - x.m[i][k]*ha;
}
}
for(int i = n-1; i >= 0; i--)
{
for(int j = i+1; j < n; j++)
x.m[i]
-= x.m[j]
*x.m[i][j];
x.m[i]
/= x.m[i][i];
}
}

int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
//    freopen("o3.txt","w",stdout);
#endif
int t,cas=1,n;
char ch[15];
scanf("%d",&t);
while(t--)
{
printf("Case %d:\n",cas++);
scanf("%d%s",&n,ch);
int len = strlen(ch)+1;
ac.clear();
ac.insert(ch);
ac.getFail();
ac.matrixmaker(n,len);
gauss(len);
printf("%lld\n",x.m[0][len]);
if(t != 0) printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: