• HDU2222 Keywords Search


    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

    本文作者:ljh2000
    作者博客:http://www.cnblogs.com/ljh2000-jump/
    转载请注明出处,侵权必究,保留最终解释权!

     
    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
     

    正解:AC自动机

    解题报告:

      AC自动机裸题。联赛前复习模板的时候突然发现AC自动机自己不会打了...好气啊

      这篇博客讲的非常详细:http://blog.csdn.net/niushuai666/article/details/7002823

      当然我的AC自动机还加了一条优化,就是当前结点,如果没有某个儿子结点,那么就直接连接上fail指针所指的结点的这一个儿子结点,这样的话在find的时候就不用每次都不停地往上跳fail指针了,可以直接一路顺着走,当全图都没有这个结点的时候,就会走到root上去,所以很方便,而且很快。

     1 //It is made by ljh2000
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #include <set>
    13 #include <string>
    14 #include <stack>
    15 using namespace std;
    16 typedef long long LL;
    17 const int MAXN = 500011;
    18 int n,cnt,tr[MAXN][26];
    19 int val[MAXN],vis[MAXN],ans;
    20 int dui[MAXN],head,tail,f[MAXN];
    21 
    22 inline int getint(){
    23     int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    24     if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
    25 }
    26 
    27 inline void read(){
    28     int u=0,now; char c;
    29     do{
    30         c=getchar(); if(c=='
    ' || c=='
    ') break;
    31         now=c-'a'; if(!tr[u][now]) tr[u][now]=++cnt;
    32         u=tr[u][now];
    33     }while(c!='-');
    34     val[u]++;
    35 }
    36 
    37 inline void getfail(){
    38     head=tail=0; dui[++tail]=0; int u,now,j;
    39     while(head<tail) {
    40         head++; u=dui[head];
    41         for(int i=0;i<26;i++) {
    42             if(tr[u][i]) {
    43                 now=tr[u][i]; j=f[u];
    44                 while(j && tr[j][i]==0) j=f[j];
    45                 f[now]= tr[j][i]==now ? 0:tr[j][i];
    46                 dui[++tail]=now;
    47             }
    48             else tr[u][i]=tr[f[u]][i];
    49         }
    50     }
    51 }
    52 
    53 inline void find(){
    54     char c; int u=0,now;
    55     while(c=getchar()) {
    56         if(c=='
    ' || c=='
    ') break;
    57         now=c-'a';    u=tr[u][now];
    58         vis[u]=1;
    59     }
    60     for(int i=tail;i>=1;i--) {
    61         u=dui[i];
    62         ans+=vis[u]*val[u];
    63         vis[f[u]]|=vis[u];
    64     }
    65 }
    66 
    67 inline void work(){
    68     int T=getint();
    69     while(T--) {
    70         cnt=0; memset(tr,0,sizeof(tr)); memset(vis,0,sizeof(vis)); memset(val,0,sizeof(val));
    71         memset(f,0,sizeof(f)); n=getint(); for(int i=1;i<=n;i++) read(); 
    72         getfail(); ans=0;
    73         find();
    74         printf("%d
    ",ans);
    75     }
    76 }
    77 
    78 int main()
    79 {
    80     work();
    81     return 0;
    82 }
  • 相关阅读:
    idea删除module
    使用腾讯云mysql的一下小坑
    docker 从 tomcat 容器连接到 mysql 容器
    数据结构开发(16):选择排序和插入排序
    数据结构开发(15):递归的思想与应用
    数据结构开发(14):KMP 子串查找算法
    数据结构开发(13):字符串类的创建
    数据结构开发(11):双向循环链表的实现
    数据结构开发(10):Linux内核链表
    数据结构开发(9):循环链表与双向链表
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/6076444.html
Copyright © 2020-2023  润新知