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): 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());
        }
    }
  • 相关阅读:
    HDU3718 Similarity 最大权值匹配
    HDU2853 Assignment 最大权值匹配+简直是太神了
    HDU3488 Tour 再次感受到KM的强大
    ZOJ2575 Full of Painting 动态规划
    ZOJ2571 Big String Outspread 模拟
    POJ3565 Ants 空间点对不相交匹配最小权值匹配
    Fleury(弗罗莱)算法求欧拉路径
    HDU2426 Interesting Housing Problem 最大权值匹配不能完成匹配的处理
    POJ1392 Ouroboros Snake 欧拉回路
    HDU2255 奔小康赚大钱 最大权值匹配
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7413260.html
Copyright © 2011-2022 走看看