首页 > 其他 > 详细

hdu[2222]keywords search

时间:2016-12-18 12:14:43      阅读:161      评论:0      收藏:0      [点我收藏+]

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

 

hdu[2222]keywords search

原文:http://www.cnblogs.com/keshuqi/p/6193861.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!