Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 66587 Accepted Submission(s): 22350
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
1 5 she he say shr her yasherhs
Sample Output
3
题意:
先给出几个单词,再给出一个字符串,统计字符串中一共出现过多少个刚刚给出的单词
解题思路:
多模式串的字符串匹配,AC自动机入门的题,代码可用来作模板,可根据代码中的注释进行理解
代码:
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#include<bits/stdc++.h>
using namespace std; #define N 500010 char str[1000010], keyword[51]; int head, tail; struct node { node *fail; ///fail指针 node *next[26]; ///下一个节点的情况 int count; ///标记是否为单词的结束 node() { ///对新建的节点进行初始化 fail = NULL; count = 0; for(int i = 0; i < 26; ++i) next[i] = NULL; } } *q[N]; node *root; void insert(char *str) ///插入单词同时建立字典树 { int temp, len; node *p = root;///每次从根节点开始遍历 len = strlen(str); for(int i = 0; i < len; ++i) { temp = str[i] - 'a'; if(p->next[temp] == NULL) p->next[temp] = new node();///如果树中不存在当前字符,就创建新的节点,存储该字符 p = p->next[temp]; } p->count++;///以这个字母结尾,进行标记 } void build_ac() ///初始化fail指针,BFS { q[tail++] = root;///根节点入队 while(head != tail) { node *p = q[head++]; ///弹出队头 node *temp = NULL; for(int i = 0; i < 26; ++i) { if(p->next[i] != NULL) { if(p == root) ///第一个元素fail必指向根, p->next[i]->fail = root; else { temp = p->fail; ///失败指针 while(temp != NULL) ///2种情况结束:匹配为空or找到匹配 { if(temp->next[i] != NULL) ///找到匹配 { p->next[i]->fail = temp->next[i]; break; } temp = temp->fail; } if(temp == NULL) ///为空则从头匹配 p->next[i]->fail = root; } q[tail++] = p->next[i]; ///上一个节点的儿子节点全部都要入队 } } } } int query() ///扫描,开始匹配 { int index, len, result; node *p = root; ///Tire入口 result = 0; len = strlen(str); for(int i = 0; i < len; ++i) { index = str[i] - 'a'; while(p->next[index] == NULL && p != root) ///失配,跳转失败指针 p = p->fail; p = p->next[index];///重新开始匹配的点, if(p == NULL) p = root; node *temp = p; ///p不动,temp计算后缀串 ///如果已经匹配了s[1.....m],用temp作为临时指针,测试s[1...m]中是否含有单词 while(temp != root && temp->count != -1) { result += temp->count; temp->count = -1; temp = temp->fail; } } return result; } int main() { int ncase, num; scanf("%d", &ncase); while(ncase--) { head = tail = 0; root = new node(); scanf("%d", &num); getchar(); for(int i = 0; i < num; ++i) { gets(keyword); insert(keyword); } build_ac(); scanf("%s", str); printf("%d\n", query()); } return 0; } |