杭电2222 Keywords Search(AC自动机)
来源:互联网 发布:假人挑战风靡网络 编辑:程序博客网 时间:2024/06/01 20:22
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 41108 Accepted Submission(s): 13079
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.
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.
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
15shehesayshrheryasherhs
Sample Output
3
Author
Wiskey
/*迷迷糊糊套模板。。加油!!!Time:2015-05-02*/#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;const int MAX=500000+10;struct Trie{ int next[MAX][26],fail[MAX],end[MAX]; int L,rt; int NewNode(){ for(int i=0;i<26;i++){ next[L][i]=-1; }end[L++]=0; return L-1; } void Init(){ L=0;rt=NewNode(); } void Insert(char *buf){ int len=strlen(buf); int cur=rt; for(int i=0;i<len;i++){ int id=buf[i]-'a'; if(next[cur][id]==-1){ next[cur][id]=NewNode(); } cur=next[cur][id]; } end[cur]++; } void Build(){ queue<int>q; int cur=rt; for(int i=0;i<26;i++){ if(next[cur][i]==-1){ next[cur][i]=rt; }else{ fail[next[cur][i]]=rt; q.push(next[cur][i]); } } while(!q.empty()){ cur=q.front();q.pop(); for(int i=0;i<26;i++){ if(next[cur][i]==-1){ next[cur][i]=next[fail[cur]][i]; }else{ fail[next[cur][i]]=next[fail[cur]][i]; q.push(next[cur][i]); } } } } int Query(char *buf){ int len=strlen(buf); int cur=rt; int ans=0; for(int i=0;i<len;i++){ cur=next[cur][buf[i]-'a']; int tmp=cur; while(tmp!=rt){ ans+=end[tmp]; end[tmp]=0; tmp=fail[tmp]; } } return ans; }};Trie AC;int main(){ int T,n; char str[MAX<<1]; scanf("%d",&T); while(T--){ scanf("%d",&n); AC.Init(); for(int i=0;i<n;i++){ scanf("%s",str); AC.Insert(str); } AC.Build(); scanf("%s",str); printf("%d\n",AC.Query(str)); }return 0;}
0 0
- 杭电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自动机)
- poj 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 2222Keywords Search (ac自动机)
- 基于FFmpeg的远程视频监控系统编解码
- android: busybox am aapt
- Markdown入门
- Simple libFM example, part1(LibFM使用方法)
- BroadcastReceiver应用详解
- 杭电2222 Keywords Search(AC自动机)
- 【Linux Mint】通过vino-server共享Cinnamon桌面
- 手机游戏中图片格式选择整理
- hdu 2531 整块bfs
- Web与数据库的简单交互
- ubuntu切换到root账户
- 最大公约数、最小公倍数算法
- android开发 - 菜单的使用
- C++数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字。