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;
    }
  • 相关阅读:
    初识ABP vNext(8):ABP特征管理
    初识ABP vNext(7):vue身份认证管理&租户管理
    初识ABP vNext(6):vue+ABP实现国际化
    初识ABP vNext(5):ABP扩展实体
    初识ABP vNext(4):vue用户登录&菜单权限
    【Flutter 混合开发】嵌入原生View-iOS
    【Flutter 混合开发】嵌入原生View-Android
    使用Flutter完成10个商业项目后的经验教训
    谷歌发布Flutter Alpha:支持Windows
    【Flutter 实战】大量复杂数据持久化
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068085.html
Copyright © 2011-2022 走看看