zoukankan      html  css  js  c++  java
  • 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
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    loj2042 「CQOI2016」不同的最小割
    loj2035 「SDOI2016」征途
    luogu2120 [ZJOI2007]仓库建设
    luogu3195 [HNOI2008]玩具装箱TOY
    51nod 1069 Nim游戏 + BZOJ 1022: [SHOI2008]小约翰的游戏John(Nim游戏和Anti-Nim游戏)
    HDU 5723 Abandoned country(最小生成树+边两边点数)
    BZOJ 1497: [NOI2006]最大获利(最大权闭合图)
    51nod 1615 跳跃的杰克
    SPOJ 839 Optimal Marks(最小割的应用)
    UVa 11107 生命的形式(不小于k个字符串中的最长子串)
  • 原文地址:https://www.cnblogs.com/pk28/p/5533085.html
Copyright © 2011-2022 走看看