zoukankan      html  css  js  c++  java
  • Hdu 2222 Keywords Search(AC自动机)

    Keywords Search
    Time Limit: 1000 MS Memory Limit:131072 K
    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

    /*
    AC自动机模板题.
    膜一下题号2333. 
    */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #define MAXN 1000001
    using namespace std;
    int n,l,tot=1,ans,fail[MAXN];
    char s[MAXN];
    bool mark[MAXN];
    struct data{int x[27],b;}tree[MAXN];
    queue<int>q;
    void Clear()
    {
        memset(mark,0,sizeof mark);
        memset(fail,0,sizeof fail);
        memset(tree,0,sizeof tree);
        ans=0;tot=1;
    }
    void add()
    {
        l=strlen(s+1);
        int now=1;
        for(int i=1;i<=l;i++)
        {
            int x=s[i]-96;
            if(!tree[now].x[x]) tree[now].x[x]=++tot;
            now=tree[now].x[x];
        }
        tree[now].b++;
        return ;
    }
    void get_fail()
    {
        for(int i=1;i<=26;i++) tree[0].x[i]=1;
        q.push(1);
        while(!q.empty())
        {
            int now=q.front();q.pop();
            for(int i=1;i<=26;i++)
            {
                if(!tree[now].x[i]) continue;
                int k=fail[now];
                while(!tree[k].x[i]) k=fail[k];//一直找,没有就指向root
                //k with father mark success and his son have i point. 
                fail[tree[now].x[i]]=tree[k].x[i];
                q.push(tree[now].x[i]);
            }
        }
        return ;
    }
    void Mark()
    {
        int now=1;l=strlen(s+1);
        for(int i=1;i<=l;i++)
        {
            int x=s[i]-96;
            mark[now]=true;
            while(!tree[now].x[x]) now=fail[now];
            now=tree[now].x[x];
            if(!mark[now])
            {
                mark[now]=true;
                int k=now;
                while(k)
                {
                    ans+=tree[k].b;
                    tree[k].b=0;
                    k=fail[k];
                }
            }
        }
        return ;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);Clear();
            for(int i=1;i<=n;i++) scanf("%s",s+1),add();
            get_fail();
            scanf("%s",s+1);
            Mark();
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    死锁及预防
    Java中的接口和抽象类
    Jmeter执行java脚本结束时提示:The JVM should have exited but did not.
    dubbo服务的group和version
    Dubbo-admin无法显示Group分组信息
    Python中的变量、引用、拷贝和作用域
    记一次调试python内存泄露的问题
    使用gdb调试python程序
    dstat用法;利用awk求dstat所有列每列的和;linux系统监控
    flask到底能登录多少用户?
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068085.html
Copyright © 2011-2022 走看看