HDU - 2222 Keywords Search (AC自动机)
来源:互联网 发布:js获取对象的长度 编辑:程序博客网 时间:2024/04/30 20:36
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 <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int kind = 26;struct Node {Node *fail;Node *next[kind];int count;Node() {fail = NULL;count = 0;for (int i = 0; i < kind; i++)next[i] = NULL;}} *q[500001];char keyword[51];char str[1000001];int head, tail;void insert(char *str, Node *root) {Node *p = root;int i = 0, index;while (str[i]) {index = str[i] - 'a';if (p->next[index] == NULL)p->next[index] = new Node();p = p->next[index];i++;}p->count++;}void buildAcAutomation(Node *root) {int i;root->fail = NULL;q[head++] = root;while (head != tail) {Node *temp = q[tail++];Node *p = NULL;for (int i = 0; i < 26; i++) {if (temp->next[i] != NULL) {if (temp == root) temp->next[i]->fail = root;else {p = temp->fail;while (p != NULL) {if (p->next[i] != NULL) {temp->next[i]->fail = p->next[i];break;}p = p->fail;}if (p == NULL)temp->next[i]->fail = root;}q[head++] = temp->next[i];}}}}int query(Node *root) {int i = 0, cnt = 0, index, len = strlen(str);Node *p = root;while (str[i]) {index = str[i] - 'a';while (p->next[index] == NULL && p != root) p = p->fail;p = p->next[index];p = (p == NULL) ? root : p;Node *temp = p;while (temp != root && temp->count != -1) {cnt += temp->count; temp->count = -1; temp = temp->fail;}i++;}return cnt;}void del(Node *root) {for (int i = 0; i < kind; i++)if (root->next[i] != NULL)del(root->next[i]);free(root);}int main() {int n, t;scanf("%d", &t);while (t--) {head = tail = 0;Node *root = new Node();scanf("%d", &n);getchar();while (n--) {gets(keyword);insert(keyword, root);}buildAcAutomation(root);scanf("%s", str);printf("%d\n", query(root));del(root);}return 0;}
2 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自动机)
- java垃圾回收机制
- 4Sum
- sql update批量替换指定字符笔记
- Textkit
- Gazebo Architecture
- HDU - 2222 Keywords Search (AC自动机)
- JSP 两种代码复用方法
- C# DataSet、DataTable、DataRow、DataColumn 数据集应用大全
- 3Sum
- 用String的format方法将日期进行格式化
- java的socket编程---telnet客户端的实现
- Java -Android 学习(第六天)
- 马云语录今天很残酷
- linux socket的select函数例子