poj 2778 AC自动机+DP+矩阵快速幂(二)

2014-11-24 11:51:21 · 作者: · 浏览: 5
j=0; j {
b[i][j] = temp[i][j];
}
}
}

void MatrixPow(type t[][size], type a[][size], int sz, int n) //矩阵的快速幂
{
while(n>0)
{
if (n&1) MatrixMultiply(t, a, sz);
MatrixMultiply(a, a, sz);
n >>= 1;
}
}
void solve(int n)
{
memset(ans,0,sizeof(ans));
for(int i=0;i ans[i][i]=1;
MatrixPow(ans,Mar,tot,n);
type res = 0;
for(int i=0; i {
if (node[i].key==0)
{
res += ans[0][i];
if (res>=mod)
res %= mod;
}
}
printf("%I64d\n",res);
}

int query(char *s)
{
int i=0,sum=0;
Node *p=root;
while(s[i])
{
while(p->next[Index(s[i])]==0&&p!=root)p=p->fail;//某个非根节点匹配失败,一直循环失败指针直到有某个字典树分支可以匹配
p=(p->next[Index(s[i])]==0) root:p->next[Index(s[i])];//一直到根也未找到满足条件的分支,则从根开始匹配
Node *t=p;
while(t!=root&&t->key!=-1)//循环失败指针,记录匹配次数
{
sum+=t->key;
t->key=-1;
t=t->fail;
}
i++;
}
return sum;
}
};
char keyword[19],str[1000009];
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)!=EOF)
{
AC_auto x;
while(m--)
{
scanf("%s",keyword);
x.insert(keyword);
}
x.build_fail();
x.get_Mar();
x.solve(n);
}
}

作者:wsniyufang