HDU---2222-Keywords Search(AC自动机)
2015-11-06 17:58
330 查看
G - Keywords Search
Crawling in process...
Crawling failed
Time Limit:1000MS
Memory Limit:131072KB
64bit IO Format:%I64d & %I64u
Submit
Status
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
Sample Output
Crawling in process...
Crawling failed
Time Limit:1000MS
Memory Limit:131072KB
64bit IO Format:%I64d & %I64u
Submit
Status
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
#include <algorithm> #include <stdio.h> #include <cstring> #include <cmath> #include <cstdlib> #include <string> #include <iostream> #include <limits.h> #include <vector> using namespace std; #define LEN 55 #define WOR 125 #define MAX 1000005 char str[MAX]; char po[LEN]; struct node { int count; node* fail; node* next[26]; node() { count=0; fail=NULL; for(int i=0; i<26; ++i) next[i]=NULL; } }; node* List[MAX]; node* root; void build_AC() { int head=0,now=0; List[head++]=root; while(head!=now) { // cout<<"fuck\n"; node* p=List[now++]; node* temp=NULL; for(int i=0; i<26; ++i) { // cout<<"fuck\n"; if(p->next[i]) { if(p==root) p->next[i]->fail=root; else { temp=p->fail; while(temp) { if(temp->next[i]) { p->next[i]->fail = temp->next[i]; break; // cout<<"this\n"; } temp=temp->fail; } if(!temp) p->next[i]->fail=root; } List[head++]=p->next[i]; } } } } int query() { int now,poi,sum=0; int len=strlen(str); node *p=root; for(int i=0; i<len; ++i) { poi=str[i]-'a'; while(!p->next[poi] && p!=root) p=p->fail; p=p->next[poi]; if(p==NULL) { p=root; //continue; } node* temp=p; while(temp!=root &&temp->count!=-1) { sum+=temp->count; temp->count=-1; temp=temp->fail; // cout<<"poi\n"; } // cout<<"niko\n"; } return sum; } void in_sert(char* s) { node* p=root; int len = strlen(s); int poi; for(int i=0; i<len; ++i) { poi=s[i]-'a'; if(!p->next[poi]) p->next[poi]=new node(); p=p->next[poi]; } p->count++; } int main() { int T,n; cin>>T; while(T--) { int i,j,k; scanf("%d",&n); root=new node(); for(i=0; i<n; ++i) { scanf("%s",po); in_sert(po); } scanf("%s",str); build_AC(); int num=query(); printf("%d\n",num); } return 0; }
相关文章推荐
- Red Hat Enterprise Linux 6.4上编译proc
- js 事件冒泡和事件捕获
- CentOS5.4安装samba服务
- 判断当前android系统的尺寸。用来区分平板和手机,亲测。
- Leetcode142: Restore IP Addresses
- build/envsetup.sh 简介
- iOS学习之Table View的简单使用(前辈写的哦,仅是收藏一下,好仔细啊,赞!)
- curl
- LeetCode OJ:Group Anagrams(同字符字符群)
- Mac 键盘快捷键
- Android支付宝、微信支付阐述
- as3将sprite类型复制到另一个sprite
- Dispose接口规范
- Hibernate缓存机制简述 (转)
- 利用GoAhead构建嵌入式web应用
- 技术面试那点事情儿
- 缓存技术(OScache)
- 反射 私有方法与异常捕捉
- Spring的三种依赖注入方式
- 判断有向图是否有圈