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;
    }
     
  • 相关阅读:
    测试一面(宇宙条)
    java实现快速排序
    java实现冒泡排序
    Perl 获取当前系统时间
    日常问题解决:记一次因watchdog未启动导致的resin启动失败解决
    日常问题解决:记一次因信号量不足引起的APACHE启动错误解决以及kernel.sem值优化
    oracle11g使用expdp、impdp导出导入用户表结构
    日常问题解决:rhel6解决curl版本过旧问题
    日常问题解决:解决fork: retry: 资源暂时不可用
    日常问题解决:rhel7修改TCP最大连接数
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9458447.html
Copyright © 2011-2022 走看看