标签:can name print ace names pac algo scanf UI
挖坑
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define N 100010
#define M 1000010
#define L 53
using namespace std;
int T,tot,n,vst[N*L];
struct node
{
int cnt,fail,trans[26];
void init()
{
cnt=fail=0;
memset(trans,0,sizeof(trans));
}
}tr[N*L];
char s[M];
void insert(char *s)
{
int len=strlen(s),u=1;
for (int i=0;i<len;i++)
{
if (!tr[u].trans[s[i]-‘a‘])
tr[tr[u].trans[s[i]-‘a‘]=++tot].init();
u=tr[u].trans[s[i]-‘a‘];
}
++tr[u].cnt;
}
void buildFail()
{
queue <int> q;
q.push(1);
while (!q.empty())
{
int u=q.front(),v,w;
for (int i=0;i<26;i++)
{
v=tr[u].fail;
while (!tr[v].trans[i])
v=tr[v].fail;
v=tr[v].trans[i],w=tr[u].trans[i];
if (w) tr[w].fail=v,q.push(w);
else tr[u].trans[i]=v;
}
q.pop();
}
}
int main()
{
for (int i=0;i<26;i++)
tr[0].trans[i]=1;
scanf("%d",&T);
for (int tt=1;tt<=T;tt++)
{
tr[tot=1].init();
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%s",s);
insert(s);
}
buildFail();
scanf("%s",s);
int len=strlen(s),now=1,tmp,ans=0;
for (int i=0;i<len;i++)
{
now=tr[now].trans[s[i]-‘a‘];
tmp=now;
while (tmp && vst[tmp]!=tt)
{
vst[tmp]=tt;
ans+=tr[tmp].cnt;
tmp=tr[tmp].fail;
}
}
printf("%d\n",ans);
}
return 0;
}
标签:can name print ace names pac algo scanf UI
原文地址:http://www.cnblogs.com/mrsheep/p/7978631.html