zoukankan      html  css  js  c++  java
  • hdu[2222]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

    Solution

    ac自动机入门题目,一次性匹配

    #include<string>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=500001;
    int n,root,dead[N],nxt[N][26],muff[N],tot,q[N<<1];
    inline int create(){
        for(int i=0;i<26;i++)
            nxt[tot][i]=-1;
        dead[tot++]=0;
        return tot-1;
    }
    inline void ins(string tar){
        int now=root;
        for(int i=0;i<tar.size();i++){
            if(nxt[now][tar[i]-'a']==-1)
                nxt[now][tar[i]-'a']=create();
            now=nxt[now][tar[i]-'a'];
        }
        dead[now]++;
    }
    inline void build(){
        int hd=1,tl=0;
        for(int i=0;i<26;i++){
            if(nxt[root][i]==-1)
                nxt[root][i]=root;
            else
                muff[nxt[root][i]]=root,
                q[++tl]=nxt[root][i];
        }
        for(;hd<=tl;hd++){
            int now=q[hd];
            for(int i=0;i<26;i++){
                if(nxt[now][i]==-1)
                    nxt[now][i]=nxt[muff[now]][i];
                else
                    muff[nxt[now][i]]=nxt[muff[now]][i],
                    q[++tl]=nxt[now][i];
            }
        }
    }
    inline int req(string tar){
        int res=0;
        for(int i=0,now=root;i<tar.size();i++){
            now=nxt[now][tar[i]-'a'];
            for(int tmp=now;tmp!=root;tmp=muff[tmp])
                res+=dead[tmp],
                dead[tmp]=0;
        }
        return res;
    }
    int main(){
        int T;
        string buf;
        ios::sync_with_stdio(false);
        cin>>T;
        for(;T;T--){
            memset(nxt,0,sizeof(nxt));
            memset(dead,0,sizeof(dead));
            memset(muff,0,sizeof(muff));
            tot=0;
            root=create();
            cin>>n;
            for(int i=0;i<n;i++)
                cin>>buf,
                ins(buf);
            build();
            cin>>buf;
            cout<<req(buf)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    【spring mvc】application context中【bean】的生命周期
    【spring mvc】基础概念
    TSql Work with comma
    统计公司人数
    t_sql中的COUNT函数
    T_SQL又另外两种找出3年连续获奖的人
    A Sql Stumper
    验证ISIN, SEDOLE和CUSIP
    按月份进行统计
    使用4中不同的方式找出连续三年获奖的人
  • 原文地址:https://www.cnblogs.com/keshuqi/p/6193861.html
Copyright © 2011-2022 走看看