zoukankan      html  css  js  c++  java
  • 【HDU2222】Keywords Search

    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

    首先要说一下这个题号,2222,hhh。

    其次这是一道AC自动机的裸题,但不知为何我第一次打AC自动机打成了爱吃自动机。。。其实这个名字也挺好

    黄学长的代码写的很优美,这里就不再多说些什么了。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 char ch[51],s[1000001];
     6 int T,n,sz,ans;
     7 int a[500001][27],q[500001],point[500001],danger[500001];
     8 bool mark[500001];
     9 
    10 void insert(){
    11     int len=strlen(ch),now=1;
    12     for (int i=0;i<len;i++){
    13         int t=ch[i]-'a'+1;
    14         if (a[now][t])now=a[now][t];
    15         else now=a[now][t]=++sz;
    16     }
    17     danger[now]++;//判断单词是否出现
    18 }
    19 
    20 void acmach(){//构建AC自动机
    21     int h=0,t=1;
    22     q[0]=1;point[1]=0;
    23     while (h<t){
    24         int now=q[h++];
    25         for (int i=1;i<=26;i++){
    26             if (!a[now][i]) continue;
    27             int k=point[now];
    28             while(!a[k][i])k=point[k];
    29             point[a[now][i]]=a[k][i];//这里是失败指针,类似KMP
    30             q[t++]=a[now][i];
    31         }
    32     }
    33 }
    34 
    35 void solve(){
    36     int len=strlen(s),k=1;
    37     for (int i=0;i<len;i++){
    38         mark[k]=1;
    39         int t=s[i]-'a'+1;
    40         while (!a[k][t])k=point[k];
    41         k=a[k][t];
    42         if (!mark[k])
    43         for (int j=k;j;j=point[j]){
    44             ans+=danger[j];
    45             danger[j]=0;//因为有多组数据
    46         }
    47     }
    48     printf("%d
    ",ans);
    49 }
    50 
    51 int main(){
    52     scanf("%d",&T);
    53     while (T--){
    54         sz=1;ans=0;
    55         scanf("%d",&n);
    56         for (int i=1;i<=26;i++)a[0][i]=1;
    57         for (int i=1;i<=n;i++){
    58             scanf("%s",ch);
    59             insert();
    60         }
    61         acmach();
    62         scanf("%s",s);
    63         solve();
    64         for(int i=1;i<=sz;i++){
    65             point[i]=danger[i]=mark[i]=0;
    66             for(int j=1;j<=26;j++)a[i][j]=0;
    67         }
    68     } 
    69 }
  • 相关阅读:
    java使用io流读取windows文件乱码问题
    java的io字符流关闭和刷新.flush();
    java使用io流实现图片复制
    java8新特性-函数式接口详细讲解及案例
    java8的lambda过滤list遍历集合,排序
    java复制对象,复制对象属性,只可复制两个对象想同的属性名。也可自定义只复制需要的属性。
    详讲KMP算法
    java栈和队列
    请求中文乱码解决
    idea创建servlet步骤
  • 原文地址:https://www.cnblogs.com/wuminyan/p/5149654.html
Copyright © 2011-2022 走看看