zoukankan      html  css  js  c++  java
  • AC自动机 HDU2222 Keywords Search

    Keywords Search

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


    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
     
     
    模板题,模板源自chaijing
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 using namespace std;
     7 int t,n,num;
     8 int len,val,j;
     9 char s[1000010];
    10 struct data{
    11     int ch[30];
    12     int fail,cnt;//cnt 以该节点结束的词的个数 
    13 }tree[1000010];
    14 queue<int>q;
    15 void clear(int x){
    16     memset(tree[x].ch,0,sizeof(tree[x].ch));
    17     tree[x].fail=0;
    18     tree[x].cnt=0;
    19 }
    20 void trie(int x){
    21     len=strlen(s);
    22     for(int i=0;i<len;i++){
    23         val=s[i]-'a'+1;
    24         if(!tree[x].ch[val]){
    25             num++;
    26             clear(num);
    27             tree[x].ch[val]=num;
    28         }
    29         x=tree[x].ch[val];
    30     }
    31     tree[x].cnt++;
    32 }
    33 void build(){
    34     while(!q.empty()) q.pop();
    35     for(int i=1;i<=26;i++) 
    36         if(tree[0].ch[i]) q.push(tree[0].ch[i]);
    37     while(!q.empty()){
    38         int p=q.front();
    39         q.pop();
    40         for(int i=1;i<=26;i++){
    41             int fail=tree[p].fail;
    42             if(tree[p].ch[i]){
    43                 
    44                 tree[tree[p].ch[i]].fail=tree[fail].ch[i];
    45                 q.push(tree[p].ch[i]);
    46             }
    47             else tree[p].ch[i]=tree[fail].ch[i];
    48         }
    49     }
    50 }
    51 int find(int x){
    52     int ans=0;
    53     len=strlen(s);
    54     for(int i=0;i<len;i++){
    55         val=s[i]-'a'+1;
    56         while(x&&!tree[x].ch[val]) x=tree[x].fail;
    57         x=tree[x].ch[val];
    58         j=x;
    59         while(j&&tree[j].cnt!=-1){
    60             ans+=tree[j].cnt;
    61             tree[j].cnt=-1;
    62             j=tree[j].fail;
    63         }
    64     }
    65     return ans;
    66 }
    67 int main(){
    68     scanf("%d",&t);
    69     while(t){
    70         t--;
    71         num=0;
    72         clear(0);
    73         scanf("%d",&n);
    74         for(int i=1;i<=n;i++){
    75             scanf("%s",s);
    76             trie(0);
    77         }
    78         build();
    79         scanf("%s",s);
    80         printf("%d
    ",find(0));
    81     }
    82     return 0;
    83 }
     
     
  • 相关阅读:
    创建了对嵌入的互操作程序集间接引用,无法嵌入互操作类型
    演练:Office 编程(C# 和 Visual Basic)
    【MySQL案件】ERROR 1418
    Quartus II 11.0破发点(不同的是低版本号)
    Arduino 数码管LED驱动器 阵列方法
    HDU1312 Red and Black 解读
    UVA714- Copying Books(最大最小化)
    最大流量dinci模板
    Python 实现类似PHP的strip_tags功能,并能够定义他们自己的一套保留标记
    从源代码分析modelDriven拦截器和params拦截器和拦截器prepare 和paramsPrepareParamsStack拦截器栈(让你的Struts2代码更简洁——如何培养框架设计能力
  • 原文地址:https://www.cnblogs.com/zwube/p/6917089.html
Copyright © 2011-2022 走看看