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

    传送门

    题面:

    Keywords Search

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

    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

        题面描述:多串匹配,裸的AC自动机,AC自动机的模板题。

        

    #include <bits/stdc++.h>
    #define maxn 1000005
    using namespace std;
    char st[maxn];
    struct Trie{
        int next[maxn][26],End[maxn],root,fail[maxn],id;
        int newnode(){
            for(int i=0;i<26;i++){
                next[id][i]=-1;
            }
            End[id]=0;
            return id++;
        }
        void init(){
            id=0;
            root=newnode();
        }
        void Insert(char *str){
            int len=strlen(str);
            int now=root;
            for(int i=0;i<len;i++){
                if(next[now][str[i]-'a']==-1){
                    next[now][str[i]-'a']=newnode();
                }
                now=next[now][str[i]-'a'];
            }
            End[now]++;
        }
        void build(){
            queue<int>que;
            int now=root;
            fail[root]=root;
            for(int i=0;i<26;i++){
                if(next[root][i]==-1){
                    next[root][i]=root;
                }
                else{
                    fail[next[root][i]]=root;
                    que.push(next[root][i]);
                }
            }
            while(!que.empty()){
                now=que.front();
                que.pop();
                for(int i=0;i<26;i++){
                    if(next[now][i]==-1){
                        next[now][i]=next[fail[now]][i];
                    }
                    else{
                        fail[next[now][i]]=next[fail[now]][i];
                        que.push(next[now][i]);
                    }
                }
            }
        }
        int query(char *str){
            int len=strlen(str);
            int now=root;
            int res=0;
            for(int i=0;i<len;i++){
                now=next[now][str[i]-'a'];
                int tmp=now;
                while(tmp!=root){
                    res+=End[tmp];
                    End[tmp]=0;
                    tmp=fail[tmp];
                }
            }
            return res;
        }
    }ac;
    int main()
    {
        int t;
        scanf("%d",&t);
    
        while(t--){
            int n;
            scanf("%d",&n);
            ac.init();
            for(int i=0;i<n;i++){
                scanf("%s",st);
                ac.Insert(st);
            }
            ac.build();
            scanf("%s",st);
            cout<<ac.query(st)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Gist
    Gist
    Gist
    汉字编码与其16进制对照
    Horizon组件安装详解
    Github目录生成器
    MVC模式网站编写经验总结
    Java多线程小结
    JGit与远程仓库链接使用的两种验证方式(ssh和https)
    Peterson算法与Dekker算法解析
  • 原文地址:https://www.cnblogs.com/Chen-Jr/p/11007299.html
Copyright © 2011-2022 走看看