zoukankan      html  css  js  c++  java
  • [HDU2222]Keywords Search|AC自动机

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 44331    Accepted Submission(s): 13933


    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 
     
    裸AC自动机,当作模板吧。
    不要用memset否则会超时……
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define T 500001
    using namespace std;
    int t,n,m,cnt,ans,a[T][27],sum[T],p[T],q[T];
    bool mark[T];
    char s[1000009],ss[51];
    inline int read()
    {
        int a=0,f=1; char c=getchar();
        while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
        while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
        return a*f;
    }
    inline void insert()
    {
        scanf("%s",ss);
        int now=1,c,l=strlen(ss);
        for (int i=0;i<l;i++)
        {
            c=ss[i]-'a'+1;
            if (a[now][c]) now=a[now][c]; else now=a[now][c]=++cnt;
        }
        sum[now]++;
    }
    void build_fail()
    {
        int t=0,w=1,now; q[1]=1; p[1]=0;
        while (t<w)
        {
            now=q[++t];
            for (int i=1;i<=26;i++)
            {
                if (!a[now][i]) continue;
                int k=p[now];
                while (!a[k][i]) k=p[k];
                p[a[now][i]]=a[k][i];
                q[++w]=a[now][i];
            }
        }
    }
    void acmach()
    {    
        scanf("%s",s);
        int now=1,c,l=strlen(s); ans=0;
        for (int i=0;i<l;i++)
        {
            mark[now]=1;
            c=s[i]-'a'+1;
            while (!a[now][c]) now=p[now];
            now=a[now][c];
            if (!mark[now])
                for (int x=now;x;x=p[x]) {ans+=sum[x]; sum[x]=0;}
        }
    }
    int main()
    {
        t=read();
        while (t--)
        {
            n=read();
            cnt=1; ans=0;
            for (int i=1;i<=26;i++) a[0][i]=1;
            for (int i=1;i<=n;i++) insert();
            build_fail();
            acmach();
            printf("%d
    ",ans);
            for (int i=1;i<=cnt;i++) 
            {
                p[i]=sum[i]=mark[i]=0;
                for (int j=1;j<=26;j++) a[i][j]=0;
            }
        }
        return 0;
    }
  • 相关阅读:
    VirtualBox Linux服务vboxservicetemplate
    oracle 11g常用命令
    haproxy dataplaneapi
    使用jproflier 分析dremio
    cube.js 支持oceanbase 的mysql driver
    fastdfs 集群异常修复实践
    使用jHiccup 分析java 应用性能
    dremio mysql arp 扩展
    cube.js graphql 支持
    apache kyuubi 参考架构集成
  • 原文地址:https://www.cnblogs.com/ws-fqk/p/4727724.html
Copyright © 2011-2022 走看看