HDU-2222文字检索

题目:
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. 

InputFirst 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. 
OutputPrint how many keywords are contained in the description.Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3
题意就是说给你一些模式串,问你有哪几个在目标串中出现过,(⊙o⊙)…,和洛谷那道末班体一模一样,就是多了一个多组数据。
 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std; int n,cnt=;
char s[];
struct Node
{
int c[],suf,flag;
void init()
{
suf=flag=;
memset(c,,sizeof(c));
}
}tree[]; void init()
{
for (int i=;i<=cnt;i++)
tree[i].init();
cnt=;
}
void Ins()
{
int head=,l=strlen(s);
for (int i=;i<l;i++)
{
int now=s[i]-'a';
if (!tree[head].c[now]) tree[head].c[now]=++cnt;
head=tree[head].c[now];
}
tree[head].flag++;
}
void Make_AC()
{
for (int i=;i<;i++) tree[].c[i]=;
int head=,tail=;
queue<int>q;
while (!q.empty()) q.pop();
tree[].suf=;
q.push();
while (!q.empty())
{
int u=q.front();
q.pop();
for (int i=;i<;i++)
if (tree[u].c[i])
{
tree[tree[u].c[i]].suf=tree[tree[u].suf].c[i];
q.push(tree[u].c[i]);
}
else tree[u].c[i]=tree[tree[u].suf].c[i];
}
}
void Solve()
{
scanf("%s",s);
int ans=,head=,len=strlen(s);
for (int i=;i<len;i++)
{
int now=s[i]-'a';
head=tree[head].c[now];
for (int j=head;j&&tree[j].flag!=-;j=tree[j].suf)
ans+=tree[j].flag,tree[j].flag=-;
}
printf("%d\n",ans);
}
int main()
{
int Cas;
scanf("%d",&Cas);
while (Cas--)
{
init();
scanf("%d",&n);
while (n--){scanf("%s",s);Ins();}
Make_AC();
Solve();
}
}
 
上一篇:代码实现SpringMvc


下一篇:山寨F407板子如何烧录正点原子例程、Keil下载提示Invalid ROM Table