zoukankan      html  css  js  c++  java
  • HDU2222 AC自动机

    Keywords Search

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


    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
     

     题意:

    给出n个模板串和一个文本串,问文本串中出现了多少个模板串。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int MAXN=1000000;
    int nod[MAXN+9][27],val[MAXN+9],f[MAXN+9],sz;
    char s[MAXN+9];
    bool vis[MAXN+9];
    void init()
    {
        sz=0;
        memset(nod[0],0,sizeof(nod));
        f[0]=val[0]=0;
        memset(vis,0,sizeof(vis));
    }
    void insert(char *s)
    {
        int len=strlen(s),rt=0;
        for(int i=0;i<len;i++){
            int id=s[i]-'a';
            if(!nod[rt][id]){
                nod[rt][id]=++sz;
                memset(nod[sz],0,sizeof(nod[sz]));
                val[sz]=0;
            }
            rt=nod[rt][id];
        }
        val[rt]++;
    }
    void get_fail()
    {
        queue<int>q;
        for(int i=0;i<26;i++){
            int u=nod[0][i];
            if(u) { q.push(u);f[u]=0; }
        }
        while(!q.empty()){
            int rt=q.front();q.pop();
            for(int i=0;i<26;i++){
                int u=nod[rt][i];
                if(!u){
                    nod[rt][i]=nod[f[rt]][i];
                    continue;
                }
                q.push(u);
                f[u]=nod[f[rt]][i];
            }
        }
    }
    int find(char *s)
    {
        int len=strlen(s),rt=0,ans=0;
        for(int i=0;i<len;i++){
            vis[rt]=1;
            int id=s[i]-'a';
            int u=nod[rt][id];
            while(u&&!vis[u]){
                vis[u]=1;
                ans+=val[u];
                u=f[u];
            }
            rt=nod[rt][id];
        }
        return ans;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int t,n;
        scanf("%d",&t);
        while(t--){
            init();
            scanf("%d",&n);
            for(int i=0;i<n;i++){
                scanf("%s",s);
                insert(s);
            }
            get_fail();
            scanf("%s",s);
            printf("%d
    ",find(s));
        }
        return 0;
    }
  • 相关阅读:
    MySQL-第十四篇事务管理
    MySQL-第十三篇使用ResultSetMetaData分析结果集
    MySQL-第十二篇管理结果集
    MySQL-第十一篇JDBC典型用法
    MySQL-第十篇多表连接查询
    Java中List集合去除重复数据的方法1
    去除List集合中的重复值(四种好用的方法)(基本数据类型可用)
    去除list集合中重复项的几种方法
    Java中List集合去除重复数据的四种方法
    Java中List集合去除重复数据的方法
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6446165.html
Copyright © 2011-2022 走看看