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;
    }
  • 相关阅读:
    【Cocos2d-X游戏实战开发】捕鱼达人之游戏场景的创建(六)
    WPF-24:绘制正多边形
    长假引起的系统审批流的变更的思考
    Linux shell编程02 shell程序的执行 及文件权限
    poj2787 算24
    REVERSE关键字之REVERSE索引
    设计模式读书笔记-----备忘录模式
    乔布斯的基本原则 (斯卡利访谈录 )
    MediaInfo源代码分析 1:整体结构
    Python 入门教程 9 ---- A Day at the Supermarket
  • 原文地址:https://www.cnblogs.com/keshuqi/p/6193861.html
Copyright © 2011-2022 走看看