zoukankan      html  css  js  c++  java
  • hdu-2222



    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自动机的模版题~~;

    代码为:


    #include<queue>  
    #include<cstdio>  
    #include<cstring>   
    #include<iostream>  
    #define MAXNODE 500005  
    using namespace std;  
    int n,T;  
    char s[1000005];  
    struct Aho-Corasick 
    {  
        int ch[MAXNODE][26];  
        int f[MAXNODE];             // fail函数  
        int val[MAXNODE];           // 是否为单词结尾    
        int last[MAXNODE];          // 后缀链接   
        int cnt[10005];             // 每个单词出现次数   
        int tot;                    // trie 单词总数   
        int num;                    // 单词出现了几个   
        int time[10005];  


        void init()  
        {  
            num = 0;  
            tot = 1;  
            memset(ch[0],0,sizeof(ch[0]));  
            memset(cnt,0,sizeof(cnt));  
            memset(time,0,sizeof(time));  
        }  


        int idx(char c)             //获取编号
        {  
            return c - 'a';  
        }  


        void insert(char *s,int v)  //插入
        {  
            int u = 0,n = strlen(s);  
            for(int i = 0;i < n;i++)  
            {  
                int c = idx(s[i]);  
                if(!ch[u][c])  
                {  
                    memset(ch[tot],0,sizeof(ch[tot]));  
                    val[tot] = 0;  
                    ch[u][c] = tot++;   
                }  
                u = ch[u][c];  
            }  
            if(val[u]) 
                time[val[u]]++;  
            else 
                val[u] = v,time[v] = 1;  
        }  


        void print(int i,int j)  
        {  
            if(j)   
            {  
                if(!cnt[val[j]]) 
                    num += time[val[j]];  
                cnt[val[j]]++;  
                print(i,last[j]);  
            }  
        }  


        void find(char *T)  
        {  
            int n = strlen(T);  
            int j = 0;  
            for(int i = 0;i < n;i++)  
            {  
                int c = idx(T[i]);  
                j = ch[j][c];  
                if(val[j]) print(i,j);  
                else if(last[j]) print(i,last[j]);  
            }  
        }  


        void getFail()  
        {  
            queue<int> q;  
            f[0] = 0;  
            for(int c = 0;c < 26;c++)  
            {  
                int u = ch[0][c];  
                if(u)  
                {  
                    f[u] = 0;  
                    q.push(u);  
                    last[u] = 0;  
                }  
            }  


            while(!q.empty())  
            {  
                int r = q.front();
                q.pop();  
                for(int c = 0;c < 26;c++)  
                {  
                    int u = ch[r][c];  
                    if(!u)  
                    {  
                        ch[r][c] = ch[f[r]][c];  
                        continue;  
                    }  
                    q.push(u);  
                    int v = f[r];  
                    f[u] = ch[v][c];  
                    last[u] = val[f[u]] ? f[u] : last[f[u]];  
                }  
            }  
        }  
    } tree;  


    int main()  
    {  
        cin>>T;
        while(T--)  
        {  
            tree.init();  
            cin>>n;  
            for(int i = 1;i <= n;i++)  
            {  
                scanf("%s",s);  
                tree.insert(s,i);  
            }  
            tree.getFail();  
            scanf("%s",s);  
            tree.find(s);  
            cout<<tree.num<<endl;  
        }  
    }   

    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
     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 
     

  • 相关阅读:
    【转】互联网科技大佬奋斗励志故事
    Java RestTemplate 请求参数字符串中有大括号{}的请求正确方法
    【资料最全】在100以内的所有情况,可以被写作三个数的立方和
    一个例子让你懂java里面的守护线程
    java中finally里面的代码一定会执行吗?(try里面做了return呢?)
    什么是mysql索引下推(有些装B面试官会问)
    java中静态变量指向的对象是在jvm那个区域?用图解告诉你。
    偶然发现在java方法中可以定义类
    Java里面的Comparable接口
    leetcode面试题 17.14. 最小K个数(快速排序,只排序一边)
  • 原文地址:https://www.cnblogs.com/csushl/p/9386575.html
Copyright © 2011-2022 走看看