hdu 2222 Keywords Search ac自己主动机
2016-02-26 14:11
405 查看
点击打开链接题目链接
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42838 Accepted Submission(s): 13488
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
Sample Output
给出n个字符串和一个模式串 问在模式串中出现了多少个给出的字符串
ac自己主动机模板题
代码:
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 42838 Accepted Submission(s): 13488
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
给出n个字符串和一个模式串 问在模式串中出现了多少个给出的字符串
ac自己主动机模板题
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; char str1[55],str2[1111111]; struct node{ node *fail,*next[26]; int ed; node(){ ed=0; fail=NULL; for(int i=0;i<26;i++) next[i]=NULL; } }*q[511111],*root; void tree_insert(char *str){ node *p=root; int l=strlen(str); int id; for(int i=0;i<l;i++){ id=str[i]-'a'; if(p->next[id]==NULL){ p->next[id]=new node(); } p=p->next[id]; } p->ed++; } void build_ac_automachine(){ int i; node *temp,*p; root->fail=NULL; int fr,ed; fr=ed=0; q[ed++]=root; while(fr<ed){ temp=q[fr++]; for(i=0;i<26;i++){ if(temp->next[i]!=NULL){ if(temp==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==NULL) temp->next[i]->fail=root; } q[ed++]=temp->next[i]; } } } } int query(char *str){ int cnt=0; int l=strlen(str); node *p=root; for(int i=0;i<l;i++){ int id=str[i]-'a'; while(p->next[id]==NULL&&p!=root) p=p->fail; p=p->next[id]; if(p==NULL) p=root; node *temp=p; while(temp!=root&&temp->ed!=-1){ cnt+=temp->ed; temp->ed=-1; temp=temp->fail; } } return cnt; } int main(){ int t,n; scanf("%d",&t); while(t--){ root=new node(); scanf("%d",&n); while(n--){ scanf("%s",str1); tree_insert(str1); } scanf("%s",str2); build_ac_automachine(); printf("%d\n",query(str2)); } return 0; }
相关文章推荐
- berkeley db write ahead logging的代码实现
- 大数据计算平台Spark内核解读
- [学点管理]党委会的工作方法,供项目经理参考
- 2016-2-16 14:12笔记
- Android SwipeRefreshLayout下拉刷新与上拉加载+滑动删除
- Eclipse插件开发之快速向导
- asp.net程序中如何使用皮肤更换的小功能
- JAVA 的 RandomAccessFile类用法
- PHP关于上传的有关配置
- JavaScript学习--Item1 严格模式
- Android studio 中 butterknife zelezny 的使用
- “APP_NAME" IS NOT TRANSLATED IN ZH, ZH_CN……..解决办法
- 23种设计模式(16):门面模式
- [leetcode] 207. Course Schedule 解题报告
- 五、UiSelector API 详细介绍
- ios - 图片处理
- Android:实现仿 美团/淘宝 多级分类菜单效果
- 20145327寒假第四周学习总结
- linux设备驱动归纳总结(八):4.总线热插拔
- Maven 知识收藏