hdu2222
来源:互联网 发布:网络语鬼畜什么意思 编辑:程序博客网 时间:2024/06/16 10:43
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 65906 Accepted Submission(s): 22053
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代码:
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<queue>#include<stdio.h>using namespace std;const int N=1e4+10,L=1e6+10;char s[L];int num;struct node{ int son[30]; int fail,cnt;}a[N*60];queue<int> q;void clear(int x){ a[x].cnt=0; a[x].fail=0; memset(a[x].son,0,sizeof(a[x].son));}void trie(char *c)//建字典树{ int l=strlen(c); int x=0; for(int i=0;i<l;i++) { int t=c[i]-'a'+1; if(!a[x].son[t]) { num++; clear(num);//刚开始所以结点都指向根结点 a[x].son[t]=num; //printf("%d\n",a[x].son[t]); } x=a[x].son[t]; //printf("%d\n",a[x].son[t]); } a[x].cnt++;}void buildAC(){ while(!q.empty()) q.pop(); for(int i=1;i<=26;i++) if(a[0].son[i]) q.push(a[0].son[i]); while(!q.empty()) { int x=q.front(); //printf("%d\n",x); q.pop(); int fail=a[x].fail; for(int i=1;i<=26;i++) { int y=a[x].son[i]; if(y) { //printf("%d\n",x); a[y].fail=a[fail].son[i]; //printf("%d\n",x); q.push(y); } else a[x].son[i]=a[fail].son[i]; } }}int find(char *c){ int l=strlen(c); int x=0,ans=0; for(int i=0;i<l;i++) { int t=c[i]-'a'+1; while(x && !a[x].son[t]) x=a[x].fail; x=a[x].son[t]; int p=x; while(p && a[p].cnt!=-1) { ans+=a[p].cnt; a[p].cnt=-1; p=a[p].fail; } } return ans;}int main(){ int t; cin>>t; while(t--) { int n; cin>>n; num=0; clear(0); for(int i=1;i<=n;i++) { cin>>s; trie(s); } buildAC(); scanf("%s",s); printf("%d\n",find(s)); } return 0;}
阅读全文
1 0
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- hdu2222
- HDU2222
- 还是hdu2222
- HDU2222 Keywords Search
- hdu2222 Keywords Search
- 禁止 cloud init 修改 hostname
- 2015 ACM Amman Collegiate Programming Contest D.Alternating Strings【Dp】
- 对一致性Hash算法,Java代码实现的深入研究
- PX4之commander剖析解读-2
- 超能粒子炮·改 HYSBZ
- hdu2222
- 文本框字数问题从http://www.cnblogs.com/beiz/p/5238224.html 处拿来的文章,谢谢北执!
- vue.js(2.0)常用指令总结以及一些指令的坑!
- 【物联网】行排式二维条码 介绍
- F
- 简单粗暴安卓全屏幕适配
- 【视频开发】ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)
- 在京单位笔试面试经验
- MapReduce中的排序和分组