您的位置:首页 > 其它

HDU 2222 Keywords Search (AC自动机入门 模板)

2015-03-06 10:42 447 查看
AC自动机入门

Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一。学习AC自动机之前得先有Trie树和KMP模式匹配算法的基础。

AC自动机算法分为3步:1.构造一棵tire树 2.构造失败指针 3.进行模式匹配

AC自动机的优化:Trie图

Keywords Search

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others

Total Submission(s): 38688 Accepted Submission(s): 12473

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



题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222

题目大意:求给定单词在文章中出现的次数

题目分析:裸的多模式匹配,ac机模板题

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int const MAX = 1e6 + 5;

struct node //Trie树
{
    int cnt;            //单词的个数
    node *next[26];     //叶子结点
    node *fail;         //失败指针
    node()              //初始化
    {
        cnt = 0;
        memset(next, NULL, sizeof(next));
        fail = NULL;
    }
};

char word[51], text[MAX];

//Trie的构造
void Insert(node *p, char *s)
{
    for(int i = 0; s[i] != '\0'; i++)
    {
        int idx = s[i] - 'a';
        if(p -> next[idx] == NULL)
            p -> next[idx] = new node();
        p = p -> next[idx];
    }
    p -> cnt ++; //表示该单词出现过,并保存出现次数
}

void AC_Automation(node *root)
{   
    queue <node*> q; //结点队列
    q.push(root);
    //得到fail指针  
    while(!q.empty())
    {
        node *p = q.front();
        q.pop();
        for(int i = 0; i < 26; i++)
        {
            if(p -> next[i])  //判断该结点是否存在
            {
                //root下第一层结点的失败指针都指向root
                if(p == root)
                    p -> next[i] -> fail = root;
                //当前结点的失败指针指向其失败结点的儿子结点
                else
                    p -> next[i] -> fail = p -> fail -> next[i];
                q.push(p -> next[i]);
            }
            else //trie图优化
            {
                if(p == root)
                    p -> next[i] = root;
                else
                    p -> next[i] = p -> fail -> next[i];
            }
        }
    }
}

int Query(node *root)
{
    int cnt = 0, len = strlen(text);
    node *p = root;
    for(int i = 0; i < len; i++)
    {
        int idx = text[i] - 'a';
        while(!p -> next[idx] && p != root)
            p = p -> fail;
        p = p -> next[idx];
        if(!p)     
            p = root;
        node *tmp = p;
        while(tmp != root)
        {
            if(tmp -> cnt >= 0)
            {
                cnt += tmp -> cnt;
                tmp -> cnt = -1;
            }
            else
                break;
            tmp = tmp -> fail;
        }
    }
    return cnt;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        node *root = new node();
        int n;
        scanf("%d", &n);
        while(n--)
        {
            scanf("%s", word);
            Insert(root, word);
        }
        AC_Automation(root);
        scanf("%s", text);
        printf("%d\n", Query(root));
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: