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;
    }

     

     

  • 相关阅读:
    String的几种初始化方法的区别
    Java编程思想之字符串
    树图 广度优先算法和深度优先算法
    bzoj1047: [HAOI2007]理想的正方形
    bzoj3124: [Sdoi2013]直径
    bzoj3930: [CQOI2015]选数
    bzoj1222: [HNOI2001]产品加工
    bzoj3578: GTY的人类基因组计划2
    bzoj4444: [Scoi2015]国旗计划
    bzoj1040: [ZJOI2008]骑士
  • 原文地址:https://www.cnblogs.com/cytus/p/9472660.html
Copyright © 2011-2022 走看看