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

     

     

  • 相关阅读:
    数据预处理之中心化(零均值化)与标准化(归一化)
    机器学习入门篇——感知器
    JAVA实现QQ聊天气泡
    设计模式学习笔记—策略模式
    JComboBox实现时间控件
    JTree实现QQ好友列表
    JAVA实现带图片的列表——JList
    JAVA与数据库MySQL相连接
    SQL语句
    HTML学习笔记——标签(二)
  • 原文地址:https://www.cnblogs.com/cytus/p/9472660.html
Copyright © 2011-2022 走看看