HDU 2222 Keywords Search(瞎搞)

Posted いいえ敗者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2222 Keywords Search(瞎搞)相关的知识,希望对你有一定的参考价值。

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 50451    Accepted Submission(s): 16236


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

 

/* ***********************************************
Author        :guanjun
Created Time  :2016/5/26 22:17:48
File Name     :2222.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long ling
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 250010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

struct ACauto{
    int ch[maxn][26];
    int sz;
    int f[maxn],last[maxn],val[maxn],cnt[maxn];
    void init(){
        sz=1;
        memset(ch[0],0,sizeof ch[0]);
        memset(cnt,0,sizeof cnt);
    }
    int idx(char c){
        return c-a;
    }
    void add(char *s,int v){
        int u=0,len=strlen(s);
        for(int i=0;i<len;i++){
            int c=idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof ch[sz]);
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=v;
    }
    void getfail(){
        queue<int>q;
        f[0]=0;
        for(int c=0;c<26;c++){
            int u=ch[0][c];
            if(u){
                f[u]=0;
                q.push(u);
                last[u]=0;
            }
        }
        while(!q.empty()){
            int r=q.front();q.pop();
            for(int c=0;c<26;c++){
                int u=ch[r][c];
                if(!u){
                    ch[r][c]=ch[f[r]][c];
                    continue;
                }
                q.push(u);
                f[u]=ch[f[r]][c];
                last[u]=val[f[u]]?f[u]:last[f[u]];
            }
        }
    }
    void print(int j){
        if(j){
            cnt[val[j]]++;
            print(last[j]);
        }
    }
    void Find(char *T){
        int n=strlen(T);
        int j=0;
        for(int i=0;i<n;i++){
            int c=idx(T[i]);
            while(j&&!ch[j][c])j=f[j];
            j=ch[j][c];
            if(val[j])print(j);
            else if(last[j])print(last[j]);
        }
    }
}ac;
char s[10010][60],T[1000010];
int BKDRHash(char* s)
{
    long long seed=131;
    long long hash=0;
    while(*s==0)s++;
    while(*s)
    {
        hash=hash*seed+(*s++);
    }
    return (hash & 0x7FFFFFFF);
}
map<int ,int>mp;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int t,n;
    cin>>t;
    while(t--){
        ac.init();
        mp.clear();
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%s",s[i]);
            ac.add(s[i],i);
            int x=BKDRHash(s[i]);
            mp[x]++;
        }
        ac.getfail();
        scanf("%s",T);
        ac.Find(T);
        int ans=0;
        for(int i=1;i<=n;i++){
            int x=ac.cnt[i];
            int y=BKDRHash(s[i]);
            if(x>0){
                ans+=mp[y];
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

以上是关于HDU 2222 Keywords Search(瞎搞)的主要内容,如果未能解决你的问题,请参考以下文章

刷题HDU 2222 Keywords Search

hdu[2222]keywords search

hdu 2222 Keywords Search

HDU - 2222 Keywords Search

hdu 2222 Keywords Search

HDU 2222 - Keywords Search