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

    /*

    Keywords Search

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


    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.
     

     */

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    using namespace std;
    int size,a[500001][27],T,shu[500001],dui[500001],fail[500001],sum;
    bool f[500001];
    char ch[100],s[1000008];
    void jia()
    {
    int l=strlen(ch),now=1;
    for(int i=0;i<l;i++)
    if(a[now][ch[i]-'a'+1])
    now=a[now][ch[i]-'a'+1];
    else
    {
    size++;
    now=a[now][ch[i]-'a'+1]=size;
    }
    shu[now]++;
    return;
    }
    void shi()
    {
    dui[1]=1;
    int h=0,t=1;
    for(;h<t;)
    {
    h++;
    int now=dui[h];
    for(int i=1;i<=26;i++)
    if(a[now][i])
    {
    int k=fail[now];
    for(;!a[k][i];k=fail[k]);
    fail[a[now][i]]=a[k][i];
    t++;
    dui[t]=a[now][i];
    }
    }
    }
    void jin()
    {
    int now=1,l=strlen(s);
    for(int i=0;i<l;i++)
    {
    f[now]=1;
    int k=s[i]-'a'+1;
    for(;!a[now][k];now=fail[now]);
    now=a[now][k];
    if(!f[now])
    for(int j=now;j;j=fail[j])
    {
    sum+=shu[j];
    shu[j]=0;
    }
    }
    printf("%d ",sum);
    }
    int main()
    {
    for(int i=0;i<=26;i++)
    a[0][i]=1;
    scanf("%d",&T);
    for(;T;T--)
    {
    size=1;
    sum=0;
    int n;
    scanf("%d",&n);
    for(;n;n--)
    {
    scanf("%s",ch);
    jia();
    }
    shi();
    scanf("%s",s);
    jin();
    for(int i=1;i<=size;i++)
    {
    f[i]=fail[i]=shu[i]=0;
    for(int j=1;j<=26;j++)
    a[i][j]=0;
    }
    }
    return 0;
    }

  • 相关阅读:
    jdb应用 远程调试
    maven POM总结
    jvm
    jdbc取出表名 名称
    nginx配置openssl证书
    DNS A记录 CNAME NS记录等的区别
    linux文件目录类命令|--cd指令
    linux文件目录类命令--ls命令
    linux文件目录类命令--pwd命令
    linux 帮助指令
  • 原文地址:https://www.cnblogs.com/xydddd/p/5147234.html
Copyright © 2011-2022 走看看