HDU 2222 Keywords Search
2016-03-22 21:53
417 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Print how many keywords are contained in the description.
[align=left]Sample Input[/align]
1
5
she
he
say
shr
her
yasherhs
[align=left]Sample Output[/align]
3
[b]AC自动机模板[/b]
View Code
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Print how many keywords are contained in the description.
[align=left]Sample Input[/align]
1
5
she
he
say
shr
her
yasherhs
[align=left]Sample Output[/align]
3
[b]AC自动机模板[/b]
#include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct tree{ int w,f; int t[26]; }t[1000001]; int tt,n,m,num=0; char s[50],c[1000001]; queue <int> q; inline void in(){ int p=0,l; for (register int i=0;i<m;i++){ l=s[i]-'a'; if (!t[p].t[l]) t[p].t[l]=++num; p=t[p].t[l]; } t[p].w++; } inline void mafa(){ register int i;int k,p; q.push(0);t[0].f=0; while(!q.empty()){ k=q.front();q.pop(); for (i=0;i<26;i++) if (t[k].t[i]){ p=t[k].f; while((!t[p].t[i])&&p) p=t[p].f; t[t[k].t[i]].f=(k==p)?0:t[p].t[i]; //printf("%d %d %d %d %d\n",i,k,p,t[t[k].t[i]].f,t[p].t[i]); q.push(t[k].t[i]); } } } inline void que(){ register int i,j; int ans=0,k=strlen(c),p=0,x; for (i=0;i<k;i++){ x=c[i]-'a'; while(!t[p].t[x]&&p) p=t[p].f; //printf("%d %d %d\n",i,p,ans); p=t[p].t[x]; if (t[p].w) for (j=p;j;j=t[j].f) ans+=t[j].w,t[j].w=0; } printf("%d\n",ans); } int main(){ register int i,j; scanf("%d",&tt); while(tt--){ for (i=0;i<=num;i++) for (j=0;j<=26;j++) t[i].t[j]=0; for (i=0;i<=num;i++) t[i].w=t[i].f=0; num=0; scanf("%d",&n); for (i=1;i<=n;i++){ scanf("%s",s); m=strlen(s); in(); } mafa(); scanf("%s",c); que(); } }
View Code
相关文章推荐
- [ZZ]实现c协程
- Android 事件总线OTTO用法快速入门
- POJ 1861 最短路
- 观察者和策略
- 欧拉计划42题
- 第6课 练习
- Offer题4 替换空格&常量字符串和字符串数组
- ExtJs之Ext.query
- 高等数学公式积累
- Java 类区别
- android布局--Android fill_parent、wrap_content和match_parent的区别
- DFS+打表
- 90 s games(90年代游戏)
- Cocoa Predicates Classes
- linux命令介绍(5)
- 基本标签(二)
- DFS+打表
- OC-如何隐藏NSLog打印的自带信息
- volatile解析(转)
- php的一些小笔记--数组