HDU【2222】Keywords Search
来源:互联网 发布:linux dhcp服务器搭建 编辑:程序博客网 时间:2024/06/13 19:19
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 47753 Accepted Submission(s): 15215
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#include <cstdio>#include <cstring>#include <iostream>#include <queue>const int maxnode = 500000;const int sigma_size = 26;using namespace std;struct Trie{ int ch[maxnode][sigma_size]; int val[maxnode]; int f[maxnode]; int flag[maxnode]; int last[maxnode]; int sz; Trie() { sz = 1; memset(ch[0],0,sizeof(ch[0])); } int idx(char c) { return c - 'a'; } void Insert(char *s) { int n = strlen(s); int u = 0; for(int i = 0; i < n ;i++) { int c = idx(s[i]); if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz] = 0; flag[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } if(val[u]) val[u]++; else val[u] = 1; } void getFail() { queue <int> q; for(int c = 0; c < sigma_size;c++) { int u = ch[0][c]; if(u) { f[u] = 0 ; q.push(u) ; last[u] = 0;} } while(!q.empty()) { int r = q.front(); q.pop(); for(int c = 0 ; c < sigma_size; c++) { int u = ch[r][c]; if(!u) continue; q.push(u); int v = f[r]; while(v && !ch[v][c]) v = f[v]; f[u] = ch[v][c]; last[u] = val[f[u]] ? f[u] : last[f[u]]; } } } int print(int j) { if(j && !flag[j]) { flag[j] = 1; return val[j]+print(last[j]); } return 0; } int Find(char * T) { getFail(); int n = strlen(T); int j = 0,sum = 0; for(int i = 0 ; i < n; i++) { int c = idx(T[i]); while(j && !ch[j][c]) j = f[j]; j = ch[j][c]; if(val[j]&&!flag[j]) sum += print(j); else if(last[j] && !flag[last[j]]) sum += print(last[j]); } return sum; } void Delete() { memset(ch,0,sizeof(ch)); memset(val,0,sizeof(val)); memset(f,0,sizeof(f)); memset(flag,0,sizeof(flag)); memset(last,0,sizeof(last)); sz = 1; }};char P[55],T[1000005];Trie tree;int main(){ int M; scanf("%d",&M); while(M--) { int N; scanf("%d",&N); while(N--) { scanf("%s",P); tree.Insert(P); } scanf("%s",T); printf("%d\n",tree.Find(T)); tree.Delete(); } return 0;}
1 0
- hdu 2222 Keywords Search
- hdu 2222 Keywords Search
- HDU-2222 Keywords Search
- hdu 2222 Keywords Search
- Hdu 2222 Keywords Search
- hdu 2222-Keywords Search
- hdu 2222 Keywords Search
- HDU 2222 Keywords Search
- HDU 2222 Keywords Search
- Hdu 2222 Keywords Search
- HDU 2222 Keywords Search
- HDU 2222 Keywords Search
- HDU 2222 Keywords Search
- hdu 2222 Keywords Search
- HDU 2222 Keywords Search
- hdu 2222 Keywords Search
- HDU 2222 Keywords Search
- HDU - 2222 Keywords Search
- 电子书《自己动手写神经网络》2016总结
- keil用fromelf生成bin文件说明
- java处理excel的2大框架:jakarta POI和JavaExcelAPI(简称JXL)
- 使用Android Studio新建Project并建立多个module
- AT指令建立TCP/IP 链接,并自收发数据
- HDU【2222】Keywords Search
- 2.Redis常用命令
- Spring整合JMS(四)——事务管理 博客分类:
- 阿里云Ubuntu服务器上部署JavaWeb项目
- LeetCode 35:Search Insert Position
- 通过Linux学习RTOS
- (3)Jfreechart柱状图自定义柱子颜色以及柱子的方向
- AFNetworking2.0源码解析<三>
- Spring整合JMS(三)——MessageConverter介绍 博客分类: