题意:
题解:
这道题跟POJ2278相似,POJ2278求的是不包含,这道题求的是包含,那么我们就算出总和减去不包含的数量就可以得出包含的数量了。思路是从博客上学到的,但是laoda的代码,可能是我太垃圾了,看着不太理解的了,所以还是自己折腾弄了一下午,终于弄出来了,折腾一下午的原因是flag和fail看错。 。。MD。。
以下是给我思路和公式的博客:
http://www.xuebuyuan.com/1154746.html
http://blog.youkuaiyun.com/runner__/article/details/51394417#
http://blog.youkuaiyun.com/alxpcun/article/details/51217002
http://blog.youkuaiyun.com/baidu_23081367/article/details/52347256
这里解释一下unsigned long long为什么超过2^64会自动mod2^64,因为 unsigned long long的范围为(0,2^64-1)而且是没负数的,所以当你到达2^64的时候就会自动转化为0。具体看一下下面这篇博客:
http://blog.youkuaiyun.com/u014800748/article/details/45439857
另外那个矩阵的公式大家去看上面那4个博客就应该能弄懂了,记得在你得出矩阵之后一定要在旁边加1列 1,就相当于弄了个E进去矩阵中了。
然后感觉诧异的就是为什么不直接算出包含的字符串呢?我自己的理解就是AC自动机自身的特点决定了要求出不包含的数量再让总数减去。因为你AC自动机有一个失配指针,可以使你在某个字母不匹配的时候转移去另外一个具有公共后缀的匹配串中,这样得出来的不匹配字符串速度快很多,而你AC自动机匹配成功之后是不会转移的,所以很难求出长度为m的包含匹配串的字符串,因为你长度为m的字符串有26^m种变化啊,求到你吐血啊。不知道本菜的解释合不合理,希望有dalao指出我的不足和不对,免得误导了大家,谢谢了。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define ull unsigned long long int
struct Trie
{
bool flag;
int id;
Trie *fail;
Trie *next[26];
Trie(){
id=0;
flag=false;
fail=0;
for(int i=0;i<26;i++)
next[i]=0;
}
}*root;
struct node
{
ull m[50][50];
node(){
memset(m,0,sizeof(m));
}
};
int tot;
Trie *dfn[50];
void insert(char *s)
{
Trie *r=root;
for(int i=0;s[i];i++)
{
int x=s[i]-'a';
if(r->next[x]==0){
r->next[x]=new(Trie);
r=r->next[x];
r->id=tot;
dfn[tot++]=r;
}
else
r=r->next[x];
}
r->flag=true;
}
void getfail()//这里要改点东西
{
queue<Trie *>q;
q.push(root);
Trie *p;
Trie *temp;
while(!q.empty())
{
temp=q.front();
q.pop();
for(int i=0;i<26;i++)
{
if(temp->next[i])
{
if(temp==root)
{
temp->next[i]->fail=root;
}
else
{
p=temp->fail;
while(p)
{
if(p->next[i])
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==0)
temp->next[i]->fail=root;
}
if(temp->next[i]->fail->flag)//儿子是危险串时,自己也应该为危险串,就相当于C是危险字符串,AC中A不是危险的,但是匹配了C成为了危险的。
temp->next[i]->flag=true;//或者可以这样理解:表示这个前缀是词根,如acg,ac.
q.push(temp->next[i]);
}
else//这一步也是关键,相当于把空的节点补充完整,使每个子串都有儿子
{
if(temp->fail) temp->next[i]=temp->fail->next[i];
else temp->next[i]=root;
}
}
}
}
node getMatrix()
{
node A;
for(int i=0;i<tot;i++)
for(int j=0;j<26;j++)
if(!dfn[i]->flag&&!dfn[i]->next[j]->flag)
A.m[dfn[i]->id][dfn[i]->next[j]->id]++;
for(int i=0;i<=tot;i++)//在原矩阵中加一列1,就相当于弄了E进去矩阵中。
A.m[i][tot]=1;
return A;
}
node cla(node A,node B)
{
node C;
for(int i=0;i<=tot;i++)
for(int j=0;j<=tot;j++)
for(int k=0;k<=tot;k++)
if(A.m[i][k]&&B.m[k][j])
{
C.m[i][j]+=A.m[i][k]*B.m[k][j];
}
return C;
}
node POW(node A,int k,int n)
{
node C;
for(int i=0;i<=n;i++) C.m[i][i]=1;
while(k)
{
if(k&1) C=cla(C,A);
A=cla(A,A);
k>>=1;
}
return C;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
root=new(Trie);
tot=0;
dfn[tot++]=root;
char c[10];
for(int i=0;i<n;i++)
{
scanf("%s",c);
insert(c);
}
getfail();
node A=getMatrix();
// for(int i=0;i<=tot;i++)
// {
// for(int j=0;j<=tot;j++)
// printf("%llu ",A.m[i][j]);
// printf("\n");
// }
A=POW(A,m,tot+1);//这里计算的是A^0+A^1 + A^2 + A^3 + ... + A^m.
ull ans1=0;
for(int i=0;i<=tot;i++)
ans1+=A.m[0][i];
node B;
B.m[0][0]=26,B.m[0][1]=B.m[1][1]=1,B.m[1][0]=0;
B=POW(B,m,2);//这里计算的是26^0+26^1 + 26^2 + 26^3 + ... +26^m.
ull ans2=0;
for(int i=0;i<2;i++)
ans2+=B.m[0][i];
// printf("%llu\n",ans2);
printf("%llu\n",ans2-ans1);
}
}