hdu_2222 Keywords Search(AC自动机)
来源:互联网 发布:阿里旺旺国际版 mac 编辑:程序博客网 时间:2024/06/05 22:48
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 58586 Accepted Submission(s): 19232
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
3Aho-Corasick自动机模板题。用cot数组保存单词出现的次数。#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct ACAutomata{ int next[maxn][26], fail[maxn]; int cot[maxn]; int root, L; int newnode() { for(int i = 0; i < 26; i++) next[L][i] = -1; cot[L++] = 0; return L-1; } void init() { L = 0; root = newnode(); } void insert(char buf[]) { int len = strlen(buf); int now = root; for(int i = 0; i < len; i++) { if(next[now][buf[i]-'a'] == -1) next[now][buf[i]-'a'] = newnode(); now = next[now][buf[i]-'a']; } cot[now]++; } void build() { queue<int> Q; fail[root] = root; for(int i = 0; i < 26; i++) { if(next[root][i] == -1) next[root][i] = root; else { fail[next[root][i]] = root; Q.push(next[root][i]); } } while(!Q.empty()) { int now = Q.front(); Q.pop(); for(int i = 0; i < 26; i++) { if(next[now][i] == -1) next[now][i] = next[fail[now]][i]; else { fail[next[now][i]] = next[fail[now]][i]; Q.push(next[now][i]); } } } } int query(char buf[]) { int res = 0; int len = strlen(buf); int now = root; for(int i = 0; i < len; i++) { now = next[now][buf[i]-'a']; int temp = now; while(temp != root) { res += cot[temp]; cot[temp] = 0; temp = fail[temp]; } } return res; }};int n;ACAutomata aca;char buf[maxn];int main(){ int T; scanf("%d", &T); while(T--) { aca.init(); scanf("%d", &n); char s[52]; for(int i = 0; i < n; i++) { scanf("%s", s); aca.insert(s); } aca.build(); scanf("%s", buf); printf("%d\n", aca.query(buf)); } return 0;}
0 0
- hdu_2222 Keywords Search(AC自动机)
- Keywords Search (HDU_2222) AC自动机
- hdu_2222: Keywords Search(AC自动机模板题)
- HDU_2222 Keywords Search 【AC自动机模板题】【动态链表】
- Keywords Search (ac 自动机)
- Keywords Search(AC自动机)
- Keywords Search(AC自动机)
- hdU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC自动机)
- HDU 2222 Keywords Search (AC自动机)
- hdu - 2222 - Keywords Search(AC自动机)
- hdu2222 Keywords Search (AC自动机)
- hdu 2222 Keywords Search(AC自动机)
- poj 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC 自动机)
- HDU 2222 - Keywords Search (AC自动机)
- HDU - 2222 Keywords Search (AC自动机)
- border-radius 详细用法解析(demo示例)绝大部分图形都有
- 133UVa救济金发放——不用链表
- 转载:Tegra TK1刷机 版本R21.4
- cocos2dx 弹出 android 对话框
- 处理BigDecimal非整除异常
- hdu_2222 Keywords Search(AC自动机)
- ROS hydro turtlebotArm相关网址总结
- 【Linux】linux常用基本命令
- day7 part2:JavaScript DOM编程学习笔记01
- 高效搜索
- java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries
- 3D打印机shader特效简单实现
- git config配置文件
- 使用adb命令连接模拟器,在5037端口未被占用情况下仍报端口错误