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;
    }
  • 相关阅读:
    Servlet第六篇【Session介绍、API、生命周期、应用、与Cookie区别】
    Servlet第五篇【介绍会话技术、Cookie的API、详解、应用】
    Servlet第四篇【request对象常用方法、应用】
    Servlet第三篇【request和response简介、response的常见应用】
    Tomcat就是这么简单
    JProfiler远程监控Linux上Tomcat的安装过程细讲(步骤非常详细!!!)
    FileChannel类的理解和使用
    Lifetime-Based Memory Management for Distributed Data Processing Systems
    Spark的核心RDD(Resilient Distributed Datasets弹性分布式数据集)
    Hadoop与Spark之间的比较
  • 原文地址:https://www.cnblogs.com/Chen-Jr/p/11007299.html
Copyright © 2011-2022 走看看