HDOJ 2222 AC自动机模板题
来源:互联网 发布:网络舆情监测系统 编辑:程序博客网 时间:2024/06/11 02:37
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 44704 Accepted Submission(s): 14109
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
写了半天,找不到哪里错了,到博客上找了篇文章。 AC自动机
然后加上AC自动机的介绍AC自动机算法详解
#include<iostream>#include<stdio.h>#include<cstring>#include<queue>using namespace std;struct node{ node *fail,*s[27]; int w;}*head;int t,n,i,j,sum;char temp[51],str[1000001];queue<node*> myqueue;node *getfail(node *p,int k){ if (p->s[k]!=NULL) return p->s[k]; else if (p==head) return head; else return getfail(p->fail,k);}void built_trie(){ node *root=head; node *tep; for (j=0;j<strlen(temp);j++){ if (root->s[temp[j]-'a']==NULL){ tep=new node; for (int k=0;k<26;k++) tep->s[k]=NULL; tep->w=0; tep->fail=head; root->s[temp[j]-'a']=tep; } root=root->s[temp[j]-'a']; if (j==strlen(temp)-1) root->w+=1; } return ;}void built_ac(){ node *root; while (!myqueue.empty()) myqueue.pop(); myqueue.push(head); while (!myqueue.empty()){//构造fail指针用BFS的方法扩展 root=myqueue.front(); myqueue.pop(); for (j=0;j<26;j++) if (root->s[j]!=NULL){ myqueue.push(root->s[j]); if (root==head) root->s[j]->fail=head; else root->s[j]->fail=getfail(root->fail,j); } } return ;}void find(){ int len=strlen(str); node* tep; node *root=head; for (j=0;j<len;j++){ while (root->s[str[j]-'a']==NULL && root!=head) root=root->fail; root=(root->s[str[j]-'a']==NULL)?head:root->s[str[j]-'a']; tep=root; while (tep!=head){//如果单词A出现过了,那么与他具有相同前缀的单词也都出现过了 sum+=tep->w; tep->w=0; tep=tep->fail; } } return ; }int main(){ scanf("%d",&t); while (t--){ sum=0; head=new node; for (i=0;i<26;i++) head->s[i]=NULL; head->fail=head; head->w=0; scanf("%d",&n); for (i=0;i<n;i++){ scanf("%s",temp); built_trie(); } built_ac(); scanf("%s",str); find(); printf("%d\n",sum); } return 0;}
0 0
- HDOJ 2222 AC自动机模板题
- HDOJ-2222 (AC自动机模板)
- HDOJ-2222 AC自动机模板题..注意细节..
- HDOJ 2222 - Keywords Search 更新AC自动机模板...用类来表示
- hdu 2222 AC自动机模板题
- hdu 2222 AC自动机模板题
- hdu 2222 ac自动机模板题
- HDU 2222 ac自动机入门模板题
- HDU 2222 AC自动机模板题
- HDU - 2222 AC自动机模板题
- hdu 2222 ac自动机模板题
- hdu 2222AC自动机模板题
- HDU 2222 ac自动机模板题
- hdu 2222(AC自动机模板)
- HDU - 2222 AC 自动机模板
- HDU 2222 AC自动机模板
- hdu 2222 ac自动机模板
- hdu 2222 ac自动机 模板
- 七夕祝福
- js 跨浏览器基础事件,浏览器检测,判断浏览器的名称、版本号、操作系统
- GPU/DRM 简介
- Jquery-Uncaught ReferenceError: $ is not defined报错
- 判断一个单向链表是否有环
- HDOJ 2222 AC自动机模板题
- 字节对齐规则
- 五域论湛
- 如何从一台客户机向服务机发起40亿TCP长连接?
- 曾国藩:从30岁起,脱胎换骨
- 金融扫盲-资本市场从天使轮、ABCD轮、风投、到上市圈钱、借壳上市。
- oracle的引用型变量和记录型变量程序举例
- Linux环境进程间通信(一)
- inno setup 5 添加快捷方式默认选中