HDU 2222 Keywords Search(ac自动机)
来源:互联网 发布:视频解析源码 编辑:程序博客网 时间:2024/06/05 08:45
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
ac自动机裸题
code :
#include <iostream>#include <bits/stdc++.h>using namespace std;//const int maxn = 1e6+10;char str[1000];char text[1000010];struct node{ int t; node * child[26]; node *fail; node() { t = 0; for (int i=0; i<26; i++) { child[i] = NULL; } fail =NULL; }};node *root;void build() //建立字典树{ //cout<<a<<endl; node *p; node * newnode; int i; p = root; for (i=0; i<strlen(str); i++) { int m = str[i]-'a'; if (p->child[m]==NULL) { newnode = new node; p->child[m] = newnode; } p = p->child[m]; } p->t++;}void getfail() //建立fail指针{ int i; node *p; p = root; queue<struct node *>qu; qu.push(p); while (!qu.empty()) { p = qu.front(); qu.pop(); for (i=0; i<26; i++) { if (p->child[i]!=NULL) { if (p==root) { p->child[i]->fail = root; } else { node * newnode= p->fail; while (newnode!=NULL) { if (newnode->child[i]!=NULL) { p->child[i]->fail = newnode->child[i]; break; } newnode = newnode->fail; } if (newnode == NULL) { p->child[i]->fail = root; } } qu.push(p->child[i]); } } }}int find_ac(){ int ans=0; node *p; p = root; int i=0; while (text[i]!='\0') { int m = text[i]-'a'; while (p->child[m]==NULL&&p!=root) { p = p->fail; } p = p->child[m]; if (p==NULL) p = root; node *newnode= p; while (newnode!=NULL&&newnode->t!=-1) { ans += newnode->t; newnode->t = -1; newnode = newnode->fail; } i++; } return ans;}void ended(node *head) //删除字典树{ for (int i=0; i<26; i++) { if (head->child[i]!=NULL) ended(head->child[i]); } delete head;}int main(){ int N; scanf("%d",&N); while (N--) { //ans = 0; int n; scanf("%d",&n); root =new node; while (n--) { scanf("%s",str); build(); } scanf("%s",text); getfail(); int ans = find_ac(); printf("%d\n",ans); ended(root); } return 0;}
- 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自动机)
- 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自动机)
- 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自动机)
- HDU 2222Keywords Search (ac自动机)
- HDU 2222 Keywords Search(AC自动机)
- HDU-2222 Keywords Search(AC自动机)
- webpack的简单配置vue项目
- 项目中用到mobiscroll的一些心得
- 正则表达式验证 只能数字或小数 只能有一个小数点并且第一位不能为小数点
- Bootstrap中col-*-offset和col-*-push的区别
- win环境下用计划任务定时执行一个php程序文件
- HDU 2222 Keywords Search(ac自动机)
- strncmp和strncat库函数的实现。
- 数组内容随机排序
- org.springframework.data.mapping.model.MappingException: No id property found for object of type
- 阿里NLP架构与应用
- 同城信息发布小程序开发源码解析
- wk2168驱动调试(三)
- JavaScript for..in 练习
- 内螺旋矩阵和双螺旋矩阵(python)