zoukankan      html  css  js  c++  java
  • HDU 2222 Keywords Search

    Keywords Search



    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
    #include<cstdio>
    #include<queue>
    #include<string>
    #include<map>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int N=1e6+5;
    const int sigma_size=26;
    const int max_node=500050;
    char text[N],p[10005][55];
    bool vis[max_node];
    int ans;
    struct AhoCorasickAutomata
    {
        int ch[max_node][sigma_size];
        int val[max_node],last[max_node],f[max_node],cnt[max_node];
        int sz;
        inline void init()
        {
            sz=1;
            memset(ch[0],0,sizeof(ch[0]));
            memset(cnt,0,sizeof(cnt));
        }
        inline void Count(int j){if(j&&!vis[j])vis[j]=1,ans+=cnt[j],Count(last[j]);}
        inline int idx(char c){return c-'a';}
        void Insert(char *s,int v)
        {
            int u=0,len=strlen(s);
            for(int i=0;i<len;i++)
            {
                int c=idx(s[i]);
                if(!ch[u][c])
                {
                    memset(ch[sz],0,sizeof(ch[sz]));
                    val[sz]=0;
                    ch[u][c]=sz++;
                }
                u=ch[u][c];
            }
            val[u]=v;
            cnt[u]++;
        }
        void Getfail()
        {
            queue<int>q;
            f[0]=0;
            for(int i=0;i<sigma_size;i++)
            {
                int u=ch[0][i];
                if(u){f[u]=0;q.push(u);last[u]=0;}
            }
            while(!q.empty())
            {
                int r=q.front();q.pop();
                for(int i=0;i<sigma_size;i++)
                {
                    int u=ch[r][i];
                    if(!u){ch[r][i]=ch[f[r]][i];continue;}
                    q.push(u);
                    int v=f[r];
                    while(v&&!ch[v][i])v=f[v];
                    f[u]=ch[v][i];
                    last[u]=(val[f[u]]?f[u]:last[f[u]]);
                }
            }
        }
        void Find(char* s)
        {
            for(int i=0,j=0,len=strlen(s);i<len;i++)
            {
                int c=idx(s[i]);
                j=ch[j][c];
                if(val[j])Count(j);
                else if(last[j])Count(last[j]);
            }
        }
    }ac;
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            ans=0;
            memset(vis,0,sizeof(vis));
            ac.init();
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%s",p[i]),ac.Insert(p[i],i);
            ac.Getfail();
            scanf("%s",text);
            ac.Find(text);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    组合两个表(sql查询语句)
    The six Day 数组中找出和为目标值
    实时监控-CPU
    使用 python 的细碎总结
    Visual Studio 2017 运行、调试使用CMake构建的多可执行程序项目
    git 学习笔记 —— 在不同的提交间进行切换和重置( git reset/reflog/tag 命令)
    git 学习笔记 —— 保留/丢弃当前分支修改并切换至其他分支
    git 学习笔记 —— 获取远端仓库以及提交信息至远端 git remote/fetch/branch
    git 学习记录—— git 中的仓库、文件状态等概念介绍
    VScode 配置 C++ 环境进行编译和调试
  • 原文地址:https://www.cnblogs.com/homura/p/5758940.html
Copyright © 2011-2022 走看看