hdu 2222(Keywords Search) (AC自动机)
2014-08-11 13:55
453 查看
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 34147 Accepted Submission(s): 11061
Problem Description
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.
Input
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.
Output
Print how many keywords are contained in the description.
Sample Input
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
Author
Wiskey
Recommend
lcy | We have carefully selected several similar problems for you: 2896 3065 2243 2825 3341
【题意】有几个单词在目标串中出现
【思路】估计是最入门的AC自动机吧,不过网上的代码有的是错的,虽然能ac,呵呵。。
discuss里的反例
12 6 a ba cba dcba baf f dcbafd
【代码】
/* kmp+tire */ #include<cstdio> #include<cstring> #include<queue> #include<map> #include<iostream> using namespace std; int const MAX_NODE=52*10100; int const MAX_SIZE=26; int tot; int ch[MAX_NODE][MAX_SIZE]; int value[MAX_NODE]; //map<int,string>II; void Build_Trie(char *word,int key) { int u=0; for(int i=0;word[i];i++) { int v=word[i]-'a'; if(!ch[u][v]) { memset(ch[tot],0,sizeof(ch[tot])); value[tot]=0; ch[u][v]=tot++; } u=ch[u][v]; } value[u]+=key; //II[u]=(string)word; } void print(int root,char *s,int len) { if(value[root]) printf("%s\n",s); for(int i=0;i<MAX_SIZE;i++) { s[len]=i+'a';s[len+1]=0; if(ch[root][i]) print(ch[root][i],s,len+1); } } int Fail[MAX_NODE]; int Ac(char *T) { int ans=0; int len=strlen(T); int j=0; for(int i=0;i<len;i++) { int u=T[i]-'a'; while(j&&!ch[j][u])j=Fail[j]; j=ch[j][u]; //cout<<"Fail j="<<j<<endl; int temp=j; while(temp)//这里可以改写成while(temp&&value[temp]!=-1) { //printf("%d\n",value[j]); if(value[temp]) { //cout<<II[temp]<<endl; ans+=value[temp]; value[temp]=0;//按上面的,就需要写成value[temp]=-1; } temp=Fail[temp]; } } return ans; } queue<int>Q; void GetFail() { Fail[0]=0; for(int c=0;c<MAX_SIZE;c++) { int u=ch[0][c]; if(u) { Fail[u]=0; Q.push(u); //last } } while(!Q.empty()) { int r=Q.front();Q.pop(); for(int c=0;c<MAX_SIZE;c++) { int u=ch[r][c]; if(!u)continue; Q.push(u); int v=Fail[r]; while(v&&!ch[v][c]) v=Fail[v]; Fail[u]=ch[v][c]; //last[u]=value[Fail[u]]?Fail[u]:last[Fail[u]]; } } } void innit() { memset(ch[0],0,sizeof(ch[0])); tot=1; value[0]=0; } char T[1000005]; int main() { int Cas; scanf("%d",&Cas); while(Cas--) { int n; scanf("%d",&n); char s[100]; innit(); while(n--) { // cout<<"n: "<<n<<endl; scanf("%s",s); Build_Trie(s,1); } //s[0]=0; //print(0,s,0); //printf("\n"); GetFail(); scanf("%s",T); int ans=Ac(T); printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU - 2222 Keywords Search (AC自动机)
- HDU 2222——Keywords Search(AC自动机)
- HDU 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search (AC自动机入门题目)
- HDU 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search (AC自动机模板题)
- HDU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search (AC自动机~)
- HDU 2222 Keywords Search (AC自动机模板题)
- HDU 2222 Keywords Search (AC自动机)
- HDU - 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search (AC自动机模版题)
- HDU 2222 Keywords Search (ac自动机)
- HDU 2222 Keywords Search (AC自动机)
- hdu 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search (AC 自动机入门)
- hdu 2222 Keywords Search ( AC 自动机)
- hdu 2222 Keywords Search(ac自动机模板题)
- HDU 2222 AC自动机