hdu 2222 Keywords Search(ac自动机水题)
来源:互联网 发布:秋天的童话 知乎 编辑:程序博客网 时间:2024/06/06 07:25
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 60242 Accepted Submission(s): 19853
Total Submission(s): 60242 Accepted Submission(s): 19853
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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222
题意:一个字符串和n个匹配串,问最多有几个可以匹配的。
思路:ac自动机,网上有很详细的介绍,我看的是:http://blog.csdn.net/mobius_strip/article/details/22549517
这题只是用来熟悉下模板的。
小问题:
数组实现线段树时不应该初始全部赋值为0,否则会超时。
代码:
#include<bits/stdc++.h>using namespace std;const int maxn=1000000+100;int tree[maxn][30],v[maxn],fail[maxn],last[maxn];int cnt,ans;char s[maxn];void init(){ cnt=0; ans=0; memset(tree[0],0,sizeof(tree[0])); memset(v,0,sizeof(v)); memset(last,0,sizeof(last)); memset(fail,0,sizeof(fail));}void build(char *T){ int n=strlen(T); int j=0; for(int i=0; i<n; i++) { int k=tree[j][T[i]-'a']; if(!k) { tree[j][T[i]-'a']=++cnt; memset(tree[cnt],0,sizeof(tree[cnt])); } j=tree[j][T[i]-'a']; } v[j]++;}void getfail(){ queue<int>q; for(int i=0; i<26; i++) { if(tree[0][i]) q.push(tree[0][i]); } while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0; i<26; i++) { int vv=tree[u][i]; if(!vv) { tree[u][i]=tree[fail[u]][i]; continue; } q.push(vv); int uu=fail[u]; while(uu&&!tree[uu][i])uu=fail[uu]; fail[vv]=tree[uu][i]; last[vv]=v[fail[vv]]?fail[vv]:last[fail[vv]]; //找到一个之后继续匹配相同前缀的项 } }}void findf(int j){ if(!j)return ; if(v[j]) { ans+=v[j]; v[j]=0; } findf(last[j]);}void solve(char* T){ getfail(); int n=strlen(T),j=0; for(int i=0;i<n;i++) { j=tree[j][T[i]-'a']; if(v[j])findf(j); else if(last[j])findf(last[j]); }}int main(){ int T; scanf("%d",&T); while(T--) { init(); int n; scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%s",s); build(s); } scanf("%s",s); solve(s); printf("%d\n",ans); } return 0;}
0 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 2222Keywords Search (ac自动机)
- HDU 2222 Keywords Search(AC自动机)
- linux命令行与shell脚本大全_第十章_构建基本脚本
- insert append 操作的注意事项
- ip拨号器
- c primer plus 4th edition Programing Exercises answer(chapter 6)
- HTML img 使用规范
- hdu 2222 Keywords Search(ac自动机水题)
- 智能电子钟
- (33)Air Band OpenCV2.4.13_Affine变换
- windows程序事件的信号状态和非信号状态
- hibernate5--3.映射关系
- android短信监听器
- thinkphp 自动验证自动完成失效
- 桥接模式
- SVN