zoukankan      html  css  js  c++  java
  • Evanyou Blog 彩带

      题目传送门

    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
     

    Author

    Wiskey


      分析:

      水一波$AC$自动机裸题。

      直接上$AC$自动机模板就行了,记得清空数组,其他别想太多就好。

      Code:

     

    //It is made by HolseLee on 14th Aug 2018
    //HDU2222
    #include<bits/stdc++.h>
    using namespace std;
    
    const int N=5e5+7;
    int T,n,m,ans,tot,fail[N],team[N],t[N][26],val[N];
    char s[N*2];
    
    void ins(char *ch)
    {
        int len=strlen(ch),root=0,id;
        for(int i=0;i<len;++i){
            id=ch[i]-'a';
            if(!t[root][id])
            t[root][id]=++tot;
            root=t[root][id];
        }
        val[root]++;
    }
    
    void getfail()
    {
        int root=0,head=0,tail=0;;
        for(int i=0;i<26;++i){
            if(t[root][i]){
                fail[t[root][i]]=0;
                team[++tail]=t[root][i];
            }
        }
        while(head<tail){
            root=team[++head];
            for(int i=0;i<26;++i){
                if(t[root][i]){
                    fail[t[root][i]]=t[fail[root]][i];
                    team[++tail]=t[root][i];
                }
                else t[root][i]=t[fail[root]][i];
            }
        }
    }
    
    void quary()
    {
        int root=0,id;
        for(int i=0;i<m;++i){
            id=s[i]-'a';
            root=t[root][id];
            for(int j=root;j&&~val[j];j=fail[j])
            ans+=val[j],val[j]=-1;
        }
    }
    
    int main()
    {
        scanf("%d",&T);
        while(T--){
            scanf("%d",&n);
            ans=tot=0;
            memset(t,0,sizeof(t));
            memset(val,0,sizeof(val));
            memset(fail,0,sizeof(fail));
            memset(team,0,sizeof(team));
            for(int i=1;i<=n;++i){
                scanf("%s",s);ins(s);
            }
            getfail();
            scanf("%s",s);
            m=strlen(s);
            quary();
            printf("%d
    ",ans);
        }
        return 0;
    }

     

     

  • 相关阅读:
    网络流24题(03)最小路径覆盖问题(二分图匹配 + 最大流)
    POJ 1161 Help Jimmy(逆向思维的DP + 记忆化搜索总结)
    UVa 11248 Frequency Hopping(最小割入门)
    HDOJ 2767 Proving Equivalences(强连通算法入门)
    POJ 1336 The KLeague(最大流)
    POJ 1459 Power Network(最大流入门)
    ZOJ 1679 Telescope(区间DP变型题)
    网络流24题(02)太空飞行计划(最大流最小割)
    POJ 1160 Post Office(抽象的二维DP)
    网络流24题(01)搭配飞行员(最大流)
  • 原文地址:https://www.cnblogs.com/cytus/p/9472660.html
Copyright © 2011-2022 走看看