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;
    }
  • 相关阅读:
    计蒜客 奇怪的国家
    计蒜客 泥塑课
    计蒜客 判断质数
    hiho #1143 : 骨牌覆盖问题·一 (运用快速幂矩阵)
    二叉树建立,先序、中序、后序遍历(c实现)
    hiho #1272 买零食 [Offer收割]编程练习赛2
    hiho #1283 hiho密码 [Offer收割]编程练习赛3
    hiho #1288 微软2016.4校招笔试题 Font Size
    hiho一下 第九十八周 搜索一·24点
    hiho一下 第九十七周 数论六·模线性方程组
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068085.html
Copyright © 2011-2022 走看看