zoukankan      html  css  js  c++  java
  • HDU 2222 Keywords Search(AC自动机)

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 76397    Accepted Submission(s): 26376


    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
     
    Author
    Wiskey
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 
     
    分析:
    就是多给模板串和一个文本串进行匹配,问你进行匹配成功的次数
    也可以进行多次kmp,但是效率不高
    KMP加字典树就是AC自动机
    还不是很理解AC自动机
    套的模板
     
    ac自动机第一题!!!
    加油!
    code:
    #include<queue>
    #include<set>
    #include<cstdio>
    #include <iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    using namespace std;
    /*
        ac自动机
    */
    struct acnode
    {
        int sum;
        acnode* next[26];
        acnode* fail;
        acnode()
        {
            for(int i =0; i<26; i++)
                next[i]=NULL;
            fail= NULL;
            sum=0;
        }
    };
    acnode *root;
    int cnt;
    acnode* newnode()
    {
        acnode *p = new acnode;
        for(int i =0; i<26; i++)
            p->next[i]=NULL;
        p->fail = NULL;
        p->sum=0;
        return p;
    }
    //插入函数
    void Insert(char *s)
    {
        acnode *p = root;
        for(int i = 0; s[i]; i++)
        {
            int x = s[i] - 'a';
            if(p->next[x]==NULL)
            {
                acnode *nn=newnode();
                for(int j=0; j<26; j++)
                    nn->next[j] = NULL;
                nn->sum = 0;
                nn->fail = NULL;
                p->next[x]=nn;
            }
            p = p->next[x];
        }
        p->sum++;
    }
    //获取fail指针,在插入结束之后使用
    void getfail()
    {
        queue<acnode*> q;
        for(int i = 0 ; i < 26 ; i ++ )
        {
            if(root->next[i]!=NULL)
            {
                root->next[i]->fail = root;
                q.push(root->next[i]);
            }
        }
        while(!q.empty())
        {
            acnode* tem = q.front();
            q.pop();
            for(int i = 0; i<26; i++)
            {
                if(tem->next[i]!=NULL)
                {
                    acnode *p;
                    if(tem == root)
                    {
                        tem->next[i]->fail = root;
                    }
                    else
                    {
                        p = tem->fail;
                        while(p!=NULL)
                        {
                            if(p->next[i]!=NULL)
                            {
                                tem->next[i]->fail = p->next[i];
                                break;
                            }
                            p=p->fail;
                        }
                        if(p==NULL)
                            tem->next[i]->fail = root;
                    }
                    q.push(tem->next[i]);
                }
            }
        }
    }
    
    //匹配函数
    
    void ac_automation(char *ch)
    {
        acnode *p = root;
        int len = strlen(ch);
        for(int i = 0; i < len; i++)
        {
            int x = ch[i] - 'a';
            while(p->next[x]==NULL && p != root)//没匹配到,那么就找fail指针。
                p = p->fail;
            p = p->next[x];
            if(!p)
                p = root;
            acnode *temp = p;
            while(temp != root)
            {
                if(temp->sum >= 0)
                    /*
                    在这里已经匹配成功了,执行想执行的操作即可,怎么改看题目需求+
                    */
                {
                    cnt += temp->sum;
                    temp->sum = -1;
                }
                else break;
                temp = temp->fail;
            }
        }
    }
    int main()
    {
        int t;
        int n;
        scanf("%d",&t);
        char c[1000005];
        while(t--)
        {
            cnt = 0;
            scanf("%d",&n);
            root = newnode();
            for(int i = 0 ; i < n; i++)
            {
                scanf("%s",c);
                Insert(c);
            }
            getfail();
            scanf("%s",c);
            ac_automation(c);
            printf("%d
    ",cnt);
        }
        return 0;
    }
     
  • 相关阅读:
    python疑难问题---5、二维列表初始化
    python疑难问题---4、python文件读写
    心得体悟帖---200701(你以为后面比当下好,其实还真的不一定)
    心得体悟帖---200701(《隐秘的角落》成熟的孩子小白)
    div垂直居中 css div盒子上下垂直居中
    iOS 7 新版微信 URL 不支持跳转 App Store 的解决方案
    HTML5 LocalStorage 本地存储
    WDCP是什么 关于WDCP的详细介绍
    前端框架用哪个好
    GWT 实现文件上传和下载
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9458447.html
Copyright © 2011-2022 走看看