In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Print how many keywords are contained in the description.
1
5
she
he
say
shr
her
yasherhs
3
裸的ac自动机
注意读懂题意:只要出现了ans+1,不管几次
坑点:字典会出现多个单词
第一次自己手打模版
好慢TAT 有待改进
然而感觉很爽
#include
#include
#include
#include
using namespace std;
#define maxn 10001
char s[10001][51],T[maxn*100];
int len[maxn*100],P[maxn*100],trie[maxn*100][26],cnt,poi,an,fail[maxn*100],temp,dog,ok[maxn*100];
void work(int dig)
{
while(!trie[poi][dig]&&poi)poi=fail[poi];
poi=trie[poi][dig];
if(!poi)return;
temp=poi;
while(temp)
{
temp=fail[temp];
if(ok[temp])an+=ok[temp],ok[temp]=0;
}
if(ok[poi])
{
an+=ok[poi],ok[poi]=0,temp=poi;
}
}
void add(int Poi,int dig)
{
if(!trie[Poi][dig])
{
trie[Poi][dig]=++cnt;dog=cnt;
while(1)
{
if(trie[fail[Poi]][dig]){fail[cnt]=trie[fail[Poi]][dig];break;}
else if(!trie[fail[Poi]][dig]&&Poi==0)break;
else Poi=fail[Poi];
}
}
else dog=trie[Poi][dig];
}
int main()
{
freopen("2222.in","r",stdin);
freopen("2222.ans","w",stdout);
int TT,L;
scanf("%d",&TT);
while(TT--)
{
cnt=0;an=0;
int n,mx=0,p=0;
memset(s,0,sizeof(s));
memset(trie,0,sizeof(trie));
memset(ok,0,sizeof(ok));
memset(T,0,sizeof(T));
memset(P,0,sizeof(P));
memset(fail,0,sizeof(fail));
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",s[i]),len[i]=strlen(s[i]),mx=max(mx,len[i]);
for(int i=1;i<=n;i++)
{
if(!trie[0][s[i][0]-'a'])trie[0][s[i][0]-'a']=++cnt;
P[i]=trie[0][s[i][0]-'a'];
if(len[i]==1)ok[P[i]]++;
}
for(int i=1;ifor(int j=1;j<=n;j++)
{
if(i'a'),P[j]=dog;
if(i==len[j]-1)ok[P[j]]++;
}
scanf("%s",T);
L=strlen(T);
for(int i=0;i'a');
printf("%d\n",an);
}
}