HDU 2222 Keywords Search

来源:互联网 发布:数据库去掉重复行 编辑:程序博客网 时间:2024/04/26 22:20

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



she 
he 
say 
shr 
her 
yasherhs

Sample Output

3

题目大意:

输入n表示接下来有n组测试数据,输入m表示接下来有m个单词,最后输入一个字符串。问字符串中含有多少个前面出现的单词。

C++

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <queue>using namespace std;const int allSon=26;char patten[60];char text[1000010];int ans;struct TrieNode{    struct TrieNode *son[allSon];    struct TrieNode *fail;    int num;}*root;TrieNode* createNode(){    TrieNode *p;    p = (TrieNode*)malloc(sizeof(TrieNode));    for(int i = 0; i < allSon; i++) p->son[i] = NULL;    p->num = 0;    p->fail = NULL;    return p;}void insertPatten(){    TrieNode *p;    p = root;    int index = 0;    while(patten[index] != '\0')    {        int lowercase = patten[index]-'a';        if(p->son[lowercase]==NULL)        {            p->son[lowercase] = createNode();        }        p = p->son[lowercase];        index++;    }    p->num++;}void build_AC_automaton(){    TrieNode *p;    p = root;    queue<TrieNode*>qu;    qu.push(p);    while(!qu.empty())    {        p = qu.front();        qu.pop();        for(int i = 0; i < allSon; i++)        {            if(p->son[i] != NULL)            {                if(p == root)                {                    p->son[i]->fail = root;                }                else                {                    TrieNode *node = p->fail;                    while(node != NULL)                    {                        if(node->son[i]!=NULL)                        {                            p->son[i]->fail = node->son[i];                            break;                        }                        node = node->fail;                    }                    if(node == NULL)                        p->son[i]->fail = root;                }                qu.push(p->son[i]);            }        }    }}void find_in_AC_automaton(){    TrieNode *p;    p = root;    int index = 0;    while(text[index] != '\0')    {        int lowercase = text[index]-'a';        while(p->son[lowercase]==NULL && p!=root)            p = p->fail;        p = p->son[lowercase];        if(p == NULL) p = root;        TrieNode *temp = p;        while(temp!=NULL && temp->num!=-1)        {            ans += temp->num;            temp->num = -1;            temp = temp->fail;        }        index++;    }}void freeNode(TrieNode *node){    if(node != NULL)    {        for(int i = 0; i < allSon; i++)            freeNode(node->son[i]);    }    free(node);}int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        root = createNode();        for(int i = 0; i < n; i++)        {            scanf("%s",patten);            insertPatten();        }        scanf("%s",text);        build_AC_automaton();        ans = 0;        find_in_AC_automaton();        printf("%d\n",ans);        freeNode(root);    }    return 0;}





原创粉丝点击