【HDU2222】Keywords Search——AC自动机基础
2016-03-17 19:37
295 查看
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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
AC自动机讲解
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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
AC自动机讲解
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <queue> #include <stack> #include <map> #include <iostream> #include <algorithm> using namespace std; const int Max =10000; typedef struct node { int cnt; node *next[26]; node *fail; node() { fail = NULL; cnt = 0; for(int i = 0;i<26;i++) next[i] = NULL; } }Tree; int n,top; char str[1000010]; void BuildTree(Tree * Root) { int len = strlen(str); for(int i = 0 ;i<len;i++) { int ans = str[i]-'a'; if(Root->next[ans]==NULL) Root->next[ans] = new node(); Root= Root->next[ans]; } Root->cnt++; } void BFS(Tree * Root)//构建fail { Tree *u,*v; queue<Tree *>Q; Q.push(Root); while(!Q.empty()) { u = Q.front(); Q.pop(); v = NULL; for(int i = 0;i<26;i++) { if(u->next[i]!=NULL) { if(u==Root)//根节点的孩子指向根节点 { u->next[i]->fail = Root; } else { v = u->fail; while(v!=NULL)//向前找最长的后缀 { if(v->next[i]!=NULL) { u->next[i]->fail = v->next[i]; break; } v =v->fail; } if(v==NULL)//如果找不到则从头匹配。 { u->next[i]->fail = Root; } } Q.push(u->next[i]); } } } } int Query(Tree *Root) { int len = strlen(str); int ans = 0; Tree * p = Root; for(int i = 0;i<len;i++) { int ant = str[i]-'a'; while(p->next[ant]==NULL&&p!=Root) p = p->fail;// 不匹配时,通过跳转 p=p->next[ant]; if(p==NULL) p =Root; Tree *temp = p; while(temp!=Root&&temp->cnt!=-1)//统计数量 { ans+=temp->cnt; temp->cnt = -1; temp = temp ->fail; } } return ans; } int main() { int T; scanf("%d",&T); Tree * Root; while(T--) { scanf("%d",&n); Root = new node(); for(int i = 0 ;i<n;i++) { scanf("%s",str); BuildTree(Root); } BFS(Root); scanf("%s",str); printf("%d\n",Query(Root)); } return 0; }
相关文章推荐
- 用C++实现高精度数的加法
- GBDT--简单理解
- 第一篇CSDN blog
- Flexpaper的参数解释
- Android Studio 插件系列一
- 微信测试号Scope参数列表错误或没有Scope权限
- C++课堂学习笔记
- 【前端】使用readline模块实现Node.js的输入输出
- 通过execve在两个进程间传递环境变量
- 获取栈中最小值函数,时间复杂度为O(1)
- HDU 1452 Happy 2004
- java实现文件预览(openoffice+swftools+flexpaper)
- 在ubuntu上使用交叉编译工具链编译arm64版busybox
- HDOJ 1016 Prime Ring Problem【深搜】
- SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax
- HDOJ 1016 Prime Ring Problem素数环【深搜】
- 面向过程(或者叫结构化)分析方法与面向对象分析方法到底区别在哪里?
- 使用PopupWindow
- 0317复利实验总结
- 复利计算三次实验总结