zoukankan      html  css  js  c++  java
  • Keywords Search(AC自动机模板)

    Keywords Search

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


    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
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <queue>
     6 #include <cstdlib>
     7 #define N 500010
     8 using namespace std;
     9 int trie[N][26],val[N],fail[N],jishu;
    10 queue<int> que;
    11 struct aho{
    12     void init(){
    13         memset(trie,0,sizeof(trie));
    14         memset(val,0,sizeof(val));
    15         memset(fail,0,sizeof(fail));
    16         jishu=0;
    17         while(!que.empty()) que.pop();
    18 
    19     }
    20     void insert(char *s){
    21         int len=strlen(s),root=0;
    22         for(int i=0;i<len;i++){
    23             int num=s[i]-'a';
    24             if(trie[root][num]==0) trie[root][num]=++jishu;
    25             root=trie[root][num];
    26         }
    27         val[root]++;  //以这个字母结尾的字符串加一
    28     }
    29     void build(){
    30         for(int i=0;i<26;i++) if(trie[0][i]) fail[trie[0][i]]=0,que.push(trie[0][i]);
    31         while(!que.empty()){
    32             int u=que.front();  que.pop();
    33             for(int i=0;i<26;i++){
    34                 if(trie[u][i]) fail[trie[u][i]]=trie[fail[u]][i],que.push(trie[u][i]);
    35                 else trie[u][i]=trie[fail[u]][i];
    36             }
    37         }
    38     }
    39     int query(char *s){
    40         int len=strlen(s);
    41         int now=0,ans=0;
    42         for(int i=0;i<len;i++){
    43             now=trie[now][s[i]-'a'];
    44             for(int j=now;j&&~val[j];j=fail[j]) ans+=val[j],val[j]=-1;
    45         }
    46         return ans;
    47     }
    48 }AC;
    49 
    50 int n,t;
    51 char s[1000005];
    52 
    53 int main(){
    54     scanf("%d",&t);
    55     while(t--){
    56         scanf("%d",&n);
    57         AC.init();
    58         for(int i=1;i<=n;i++) scanf("%s",s),AC.insert(s);
    59         AC.build();
    60         scanf("%s",s);
    61         printf("%d
    ",AC.query(s));
    62     }
    63     return 0;
    64 }
    AC自动机模板
  • 相关阅读:
    QT 捕获事件(全局拦截)
    QT语言翻译
    QT,QLabel添加超链接
    解决VS+Qt不生成moc文件问题
    c++编译错误:invalid new-expression of abstract class type
    C++ 异常处理
    MATLAB 随机生成互不重叠的多个矩形
    sequential minimal optimization,SMO for SVM, (MATLAB code)
    How to decide on the correct number of clusters?
    python 将数据随机分为训练集和测试集
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11436762.html
Copyright © 2011-2022 走看看