HDU 2222 Keywords Search AC自动机
来源:互联网 发布:中国房地产 知乎 编辑:程序博客网 时间:2024/05/16 21:19
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35631 Accepted Submission(s): 11483
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
Recommend
lcy | We have carefully selected several similar problems for you: 2896 3065 2243 2825 3341
今天刚刚学了一下AC自动机,看了好久还是有点不太明白,那个fail指针的构造没看懂,照着别人的模板敲了一遍,先留着,以后慢慢领悟。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1005#define MAXN 10000000#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;struct node{ int cnt;//标记该单词是否为最后一个节点 node *fail;//失败指针 node *next[26];//Tire每个节点的子节点,大小根据题意而定}*q[MAXN];char keyword[51];//输入的单词,模式串char str[1000010];//主串int head,tail;node *root;void Insert(char *word,node *root){ int index,len; node *p=root,*newnode; len=strlen(word); for (int i=0;i<len;i++) { index=word[i]-'a'; if (!p->next[index]) //检查该节点是否存在,否则加入trie树 { newnode=(struct node*)malloc(sizeof(struct node));//申请空间,添加新的节点 for (int j=0;j<26;j++) newnode->next[j]=NULL; newnode->cnt=0; newnode->fail=NULL; p->next[index]=newnode; } p=p->next[index]; } p->cnt++;//单词结尾节点自增标记}void build_ac_automation(node *root){ head=0; tail=1; q[head]=root; node *temp,*p; while (head<tail) //bfs构造Trie树的失败指针 { //注意,匹配失败时,由fail指针回溯到正确的位置 temp=q[head]; head++; for (int i=0;i<26;i++) { if (temp->next[i]) { if (temp==root)//root下的第一层节点的fail指针都指向root temp->next[i]->fail=root; else { p=temp->fail; while (p) { if (p->next[i]) { temp->next[i]->fail=p->next[i]; break; } p=p->fail; } if (!p) temp->next[i]->fail=root; } q[tail++]=temp->next[i]; } } }}int query(node *root){ int i,num=0,index,len=strlen(str); node *p=root; for (i=0;i<len;i++) { index=str[i]-'a'; //由失败指针回溯寻找,判断str[i]是否存在于Trie树中 while(!p->next[index]&&p!=root) p=p->fail; p=p->next[index];//找到后p指向该节点 if (!p) p=root; node *temp=p; while (temp!=root) { if (temp->cnt>=0)//判断该节点是否被访问过 { num+=temp->cnt; temp->cnt=-1; } else break; temp=temp->fail; } } return num;}int main(){ int t,n,ans; scanf("%d",&t); while (t--) { root=(struct node *)malloc(sizeof(struct node)); for (int i=0;i<26;i++) root->next[i]=NULL; root->fail=NULL; root->cnt=0; scanf("%d",&n); for (int i=0;i<n;i++) { scanf("%s",keyword); Insert(keyword,root); } build_ac_automation(root); scanf("%s",str); ans=query(root); printf("%d\n",ans); } return 0;}
30 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自动机)
- Map和Reduce个数设置问题
- More On UIExtensionSites (Introduction To CAB/SCSF Part 14)
- BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理
- 一致性哈希算法(Consistent Hashing)
- android + maven + eclipse 开发环境搭建(windows) 一
- HDU 2222 Keywords Search AC自动机
- Introduction To SmartParts And Workspaces (Introduction To CAB/SCSF Part 15)
- 高精度乘法 【C++版(简单模拟版和FFT快速版)和java版】
- 树莓派开发系列教程10——树莓派spi液晶屏支持(fbtft)
- 【翻译】Basic MFC Reversing
- linux内存管理总结之内存分配
- extended-insert对mysqldump及导入性能的影响
- 开始写博客!!今天第一个:FFmpeg解码网络rtsp流的一般流程和这几天遇到的问题,尤其是avformat_input_open解析错误的网络串流长时间不返回!
- Android仿iPhone晃动撤销输入功能(微信摇一摇功能)