首页 > 其他 > 详细

hdu2222 Keywords Search

时间:2017-10-24 23:54:51      阅读:312      评论:0      收藏:0      [点我收藏+]

 指针我一般都会出错,所以还是自己写数组版本。

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. 

InputFirst 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. 
OutputPrint how many keywords are contained in the description.Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=1000010;
int Next[maxn][26],End[maxn],fail[maxn];
int cnt,root=1,ans;//root不能为0 
void _init()
{
     memset(Next,0,sizeof(Next));
     memset(End,0,sizeof(End));
     memset(fail,0,sizeof(fail));
     ans=0;cnt=1;
}
void _insert(char s[])
{
    int L=strlen(s);
    int now=root;
    for(int i=0;i<L;i++){
        if(!Next[now][s[i]-a]) Next[now][s[i]-a]=++cnt;
        now=Next[now][s[i]-a];
    }
    End[now]++;
}
void _build()//bfs 
{
    queue<int>q;
    q.push(root);
    while(!q.empty()){
        int Now=q.front();q.pop();
        for(int i=0;i<26;i++){
            if(Next[Now][i]){
                if(Now==root) fail[Next[Now][i]]=root;
                else{
                    int p=fail[Now];
                    while(p){//给儿子们找对象 
                        if(Next[p][i]){
                            fail[Next[Now][i]]=Next[p][i];
                            break;//找到了就停止 
                        }
                        p=fail[p];
                    }
                    if(!p) fail[Next[Now][i]]=root;//不晓得? 
                }
                q.push(Next[Now][i]);
            }
        }    
    }
}
void _query(char s[])
{
    int L=strlen(s);
    int Now=root;
    for(int i=0;i<L;i++){
          int x=s[i]-a;
          while(Now!=root&&!Next[Now][x]) Now=fail[Now];
          Now=Next[Now][x];
          if(!Now) Now=root;//不晓得? 
          int tmp=Now;
          while(tmp!=root){
                if(End[tmp]>=0){
                   ans+=End[tmp];
                   End[tmp]=-1;//避免重复 
                }
                else break;
                tmp=fail[tmp];
          }
    }
}
int main()
{
    char s[55];
    char c[maxn] ;
    int n,j,i,T;
    scanf("%d",&T);
    while(T--){
        
        _init();
        scanf("%d",&n);
        for(i=1;i<=n;i++){
           scanf("%s",s);
           _insert(s);//单词 
        }
        scanf("%s",c);
        
        _build();
        _query(c);//文章 
    
        printf("%d\n",ans);
   
    } 
    return 0;
}

 

hdu2222 Keywords Search

原文:http://www.cnblogs.com/hua-dong/p/7726311.html

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