• 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;
    }
  • 相关阅读:
    sqlserver 行转列 语文,数学,物理,化学
    除去字符串中不相临的重复的字符 aabcad 得 aabcd
    Mvc项目实例 MvcMusicStore 五
    c# 计算两日期的工作时间间隔(排除非工作日)及计算下一个工作时间点.
    html-----016---HTTP 状态消息
    html-----015---HTML ASCII 参考手册
    html-----014---统一资源定位器
    html-----013----实体字符/HTML URL 编码
    html-----012---颜色的改变
    html-----011--子窗体iframe
  • 原文地址:https://www.cnblogs.com/Chen-Jr/p/11007299.html
Copyright © 2020-2023  润新知