• HDU-2222 Keywords Search(AC自动机)


    Keywords Search

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


    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

     题意大概就是问模式串在主串中出现了几次,要注意的是模式串可能会重复,重复的也要算一个

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<queue>
    using namespace std;
    
    const int N = 1000000 * 5;
    const int maxn = 26;
    struct Aho_Corasick{
        int next[N][maxn], fail[N], data[N];
        int root, sz, cnt;
        int newnode(){
            for(int i = 0; i < maxn; i++) next[sz][i] = -1;
            data[sz] = 0;
            return sz++;
        }
        void Init() {cnt = sz = 0; root = newnode(); }
        int lx(char c) { return c-'a';}
        void Insert(char *str, int v){
            int len = strlen(str), u = root, c;
            for(int i = 0; i < len; i++){
                c = lx(str[i]);
                if(next[u][c] == -1)
                    next[u][c] = newnode();
                u = next[u][c];
            }
            data[u]++;
        }
        void Build(){
            queue<int> Q;
            fail[root] = root;
            for(int i = 0; i < maxn; i++){
                if(next[root][i] == -1) next[root][i] = root;
                else{
                    fail[next[root][i]] = root;
                    Q.push(next[root][i]);
                }
            }
            while(!Q.empty()){
                int u = Q.front(); Q.pop();
                for(int i = 0; i < maxn; i++){
                    if(next[u][i] == -1) next[u][i] = next[fail[u]][i];
                    else{
                        fail[next[u][i]] = next[fail[u]][i];
                        Q.push(next[u][i]);
                    }
                }
            }
        }
        void Query(char *str, int v){
            int len = strlen(str), u = root, c;
            for(int i = 0; i < len; i++){
                c = lx(str[i]);
                u = next[u][c];
                int tmp = u;
                while(tmp != root){
                    if(data[tmp] != -1){
                        cnt += data[tmp];
                        data[tmp] = -1;
                    }
                    tmp = fail[tmp];
                }
            }
        }
        int ANS(){ return cnt;}
    
    }AC;
    
    char str[1000005];
    
    int main(){
        int T;
        scanf("%d", &T);
        while(T--){
            int n;
            AC.Init();
            scanf("%d", &n);
            for(int i = 1; i <=n; i++){
                scanf("%s", str);
                AC.Insert(str, i);
            }
            AC.Build();
            scanf("%s", str);
            AC.Query(str, n);
            printf("%d
    ", AC.ANS());
        }
    }
  • 相关阅读:
    网页中15秒后重新发送验证码,多少秒后发送验证码
    javascript 数组去重 unique
    微信支付和微信支付通知基于sdk的说明
    php对象序列化和cookie的问题,反序列化false
    php对象序列化总出错false
    图片懒加载lazyload.js详解
    手机联动地址选择框,移动端地址联动
    Java类加载原理解析(转)
    location对象介绍
    SpringMVC使用fastjson自定义Converter支持返回jsonp格式(转)
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7413260.html
Copyright © 2020-2023  润新知