您的位置:首页 > 其它

HDU2222 Keywords Search(AC自动机)

2015-11-24 16:33 393 查看

Keywords Search

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 46676 Accepted Submission(s): 14858


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
Print how many keywords are contained in the description.

[align=left]Sample Input[/align]

1
5
she
he
say
shr
her
yasherhs

[align=left]Sample Output[/align]

3

[align=left]Author[/align]
Wiskey

[align=center] [/align]
【思路】

AC自动机模板的直接应用。

需要注意的有:

    1)
要用val纪录该节点对应得单词数目,因为可能会有重复的串。

    2)
Val累计入cnt后清0,print可能会重复遍历一个相同的单词。

【代码】

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std;

const int maxn = 10000+10;
const int maxl = 50+5;
const int maxnode = maxl*maxn;
const int sigma = 26;

struct ACautomaton {
int ch[maxnode][sigma];
int f[maxnode],val[maxnode],last[maxnode];
int sz,cnt;

void clear() {
sz=1; cnt=0;
memset(ch[0],0,sizeof(ch[0]));
val[0]=f[0]=0;
}
int idx(char c) {  return c-'a';  }
void insert(char* s) {
int n=strlen(s),u=0;
for(int i=0;i<n;i++) {
int c=idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]++;
}
void print(int j) {
if(j) {
cnt+=val[j];
val[j]=0;                //一定要置为0防止重复累计
print(last[j]);
}
}
void find(char* s) {
int n=strlen(s),j=0;
for(int i=0;i<n;i++) {
int c=idx(s[i]);
while(j && !ch[j][c]) j=f[j];
j=ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
}
void get_Fail() {
queue<int> q;
f[0]=0;
for(int c=0;c<sigma;c++) {
int u=ch[0][c];
if(u)  {  f[u]=last[u]=0; q.push(u);  }
}
while(!q.empty()) {
int r=q.front(); q.pop();
for(int c=0;c<sigma;c++) {
int u=ch[r][c];
if(!u) continue;
q.push(u);
int v=f[r];
while(v && !ch[v][c]) v=f[v];
f[u]=ch[v][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
}ac;

int n;

int main() {
//freopen("in.in","r",stdin);
//freopen("out_me.out","w",stdout);
int T;
scanf("%d",&T);
while(T--) {
char s[maxl];
scanf("%d",&n);
ac.clear();
FOR(i,1,n) {
scanf("%s",s);
ac.insert(s);
}
ac.get_Fail();
char T[1000010];
scanf("%s",T);
ac.find(T);
printf("%d\n",ac.cnt);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: