HDU 2222 AC自动机
来源:互联网 发布:淘宝宝贝视频制作 编辑:程序博客网 时间:2024/05/21 08:42
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 46407 Accepted Submission(s): 14762
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.
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.
15shehesayshrheryasherhs
3学习别人代码#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct trie{ int num; int next[26],fail; trie():fail(0),num(0){memset(next,0,sizeof(next));}} tree[250007];int root,tot;bool visited[250007];
char s[1000010];
void build(int root){ int p=root; for (int i=0;s[i]!='\0';i++) { if (tree[p].next[s[i]-'a']==0) tree[p].next[s[i]-'a']=++tot; p=tree[p].next[s[i]-'a']; } tree[p].num++;}
int que[500007];void build_ac_automatlon(int root){ int p=root; tree[p].fail=0; int f=0,r=1; que[1]=p; while (f<r) { p=que[++f]; for (int i=0;i<26;i++) if (tree[p].next[i]!=0) { que[++r]=tree[p].next[i]; if (p==root) tree[tree[p].next[i]].fail=root; else { int temp=tree[p].fail; while (temp) { if (tree[temp].next[i]!=0) { tree[tree[p].next[i]].fail=tree[temp].next[i]; break; } temp=tree[temp].fail; } if (temp==0) tree[tree[p].next[i]].fail=root; } } }} int acauto(int root){ int ans=0; int p=root; for (int i=0;s[i]!='\0';i++) { ans+=tree[p].num; tree[p].num=0; if (tree[p].next[s[i]-'a']!=0) p=tree[p].next[s[i]-'a']; else { if (p==root) continue; p=tree[p].fail; while (p!=0) { ans+=tree[p].num; tree[p].num=0; if (tree[p].next[s[i]-'a']) { p=tree[p].next[s[i]-'a']; break; } p=tree[p].fail; } if (p==0) p=root; } if (visited[p]) continue; visited[p]=true; int q=p; while (q!=0) { ans+=tree[q].num; tree[q].num=0; q=tree[q].fail; } } return ans; }
int main(){ int T; scanf("%d",&T); while (T-->0) { memset(tree,0,sizeof(tree)); memset(visited,0,sizeof(visited)); root=1; tot=1; int n; scanf("%d",&n); getchar(); for (int i=1;i<=n;i++) { scanf("%s",s); build(root); } build_ac_automatlon(root); scanf("%s",s); cout<<acauto(root)<<endl; } return 0;}
- AC自动机 hdu 2222
- HDU 2222(AC 自动机)
- hdu 2222 AC自动机
- HDU 2222 AC自动机
- Hdu 2222 [AC自动机]
- hdu 2222 AC自动机 。。
- hdu 2222 AC自动机
- hdu 2222 ac自动机
- hdu 2222 AC自动机
- HDU 2222 AC自动机
- HDU 2222 AC自动机
- hdu 2222 ac自动机
- hdu 2222 AC自动机
- ac自动机 hdu 2222
- hdu 2222 AC自动机
- hdu 2222 ac自动机
- hdu 2222 AC自动机
- HDU 2222 AC自动机
- poj3468(线段树||树状数组)
- Head First Java勘误
- Unity Sharder帧动画
- 软件使用技巧2:EXCEL工作表保护密码的撤销
- 被驱动表 拼接列无法走索引
- HDU 2222 AC自动机
- cropper本地预览 裁剪,上传到服务器
- Redis安装详细教程
- 正则表达式
- 使用Visual Studio 2013进行单元测试
- 在Ubuntu 14.04 LTS系统中设置Apache虚拟主机
- 从技术经理的角度算一算,如何可以多快好省的做个App(一)
- 为dialog添加单选列表控件
- 刷题记录 2015.11.14