HDU 2222 Keywords Search
2013-07-10 14:42
267 查看
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25356 Accepted Submission(s): 8280
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Print how many keywords are contained in the description.
[align=left]Sample Input[/align]
1
5
she
he
say
shr
her
yasherhs
[align=left]Sample Output[/align]
3
题目大意:输入一组字符串,最后一行输入一个字符串,问在最后一行的字符串中前面的字符串出现了几个。
解题方法:AC自动机。
#include <stdio.h> #include <iostream> #include <string.h> using namespace std; #define N 500010 char str[1000010]; typedef struct node { int nCount; node *fail; node *next[26]; node() { nCount = 0; fail = NULL; for (int i = 0; i < 26; i++) { next[i] = NULL; } } }TreeNode; TreeNode *Queue ; void Insert(TreeNode *pHead, char Keyword[]) { int nLen = strlen(Keyword); TreeNode *p = pHead; for (int i = 0; i < nLen; i++) { int index = Keyword[i] - 'a'; if (p->next[index] == NULL) { p->next[index] = new TreeNode; } p = p->next[index]; } p->nCount++; } void BuildAC(TreeNode *pRoot) { int head = 0, tail = 0; Queue[tail++] = pRoot; while(tail != head) { TreeNode *p = Queue[head++]; for (int i = 0; i < 26; i++) { if (p->next[i] != NULL) { if (p == pRoot) { p->next[i]->fail = pRoot; } else { TreeNode *temp = p->fail; while(temp != NULL) { if (temp->next[i] != NULL) { p->next[i]->fail = temp->next[i]; break; } temp = temp->fail; } if (temp == NULL) { p->next[i]->fail = pRoot; } } Queue[tail++] = p->next[i]; } } } } int Query(TreeNode *pRoot) { int result = 0; int nLen = strlen(str); TreeNode *p = pRoot; for (int i = 0; i < nLen; i++) { int index = str[i] - 'a'; while(p != pRoot && p->next[index] == NULL) { p = p->fail; } p = p->next[index]; if (p == NULL) { p = pRoot; } TreeNode *temp = p; while(temp != pRoot && temp->nCount != -1) { result += temp->nCount; temp->nCount = -1; temp = temp->fail; } } return result; } void DeleteNode(TreeNode *pHead) { for (int i = 0; i < 26; i++) { if (pHead != NULL) { DeleteNode(pHead->next[i]); } } delete pHead; } int main() { char Keyword[55]; int nCase; TreeNode *pHead = NULL; scanf("%d", &nCase); int n; while(nCase--) { scanf("%d", &n); pHead = new TreeNode; for (int i = 0; i < n; i++) { scanf("%s", Keyword); Insert(pHead, Keyword); } BuildAC(pHead); scanf("%s", str); printf("%d\n", Query(pHead)); DeleteNode(pHead); } return 0; }
相关文章推荐
- hdu 2222 Keywords Search (ac_automaton)
- HDU-2222 Keywords Search
- HDU 2222 Keywords Search
- HDU 2222 Keywords Search
- HDU2222---Keywords Search
- hdu 2222 Keywords Search (AC自动机~)
- hdu2222 Keywords Search
- HDU 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search
- 【hdu 2222】Keywords Search 中文题意&题解&代码(C++)
- HDU 2222 Keywords Search
- HDU 2222 Keywords Search
- 【hdu 2222】Keywords Search
- HDU 2222 Keywords Search (AC 自动机入门)
- HDU 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search
- Hdu 2222 Keywords Search
- hdu 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search