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

    Keywords Search
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 77486    Accepted Submission(s): 26859

    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

    C/C++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <vector>
     5 #include <string>
     6 #include <cstdio>
     7 #include <cstring>
     8 #include <climits>
     9 #include <iostream>
    10 #include <algorithm>
    11 #define INF 0x3f3f3f3f
    12 using namespace std;
    13 const int MAX = 5e5 + 10, MAX_L = 1e6 + 10;
    14 
    15 int n, t, rt;
    16 char ss[MAX_L];
    17 
    18 struct node
    19 {
    20     int next[26], cnt, fail;
    21 } s[MAX];
    22 
    23 void my_insert(char *ss)
    24 {
    25     int now = 0;
    26     for (int i = 0;  ss[i]; ++ i)
    27     {
    28         if (s[now].next[ss[i] - 'a'] == 0)
    29             s[now].next[ss[i] - 'a'] = rt ++;
    30         now = s[now].next[ss[i] - 'a'];
    31     }
    32     s[now].cnt ++;
    33 }
    34 
    35 void getfail()
    36 {
    37     int now, v, temp;
    38     queue <int> qu;
    39     qu.push(0);
    40     while (qu.size())
    41     {
    42         now = qu.front();
    43         qu.pop();
    44         for (int i = 0; i < 26; ++ i)
    45         {
    46             if (v = s[now].next[i])
    47             {
    48                 if (now == 0) s[v].fail = 0;
    49                 else
    50                 {
    51                     temp = s[now].fail;
    52                     while (temp && s[temp].next[i] == 0)
    53                         temp = s[temp].fail;
    54                     s[v].fail = s[temp].next[i];
    55                 }
    56                 qu.push(v);
    57             }
    58         }
    59     }
    60 }
    61 
    62 int ac(char * ss)
    63 {
    64     int v, now = 0, sum = 0;
    65     getfail();
    66     for (int i = 0; ss[i]; ++ i)
    67     {
    68         while (now && s[now].next[ss[i] - 'a'] == 0)
    69             now = s[now].fail;
    70         v = now = s[now].next[ss[i] - 'a'];
    71         while (s[v].cnt)
    72         {
    73             sum += s[v].cnt;
    74             s[v].cnt = 0;
    75             v = s[v].fail;
    76         }
    77     }
    78     return sum;
    79 }
    80 
    81 int main()
    82 {
    83     scanf("%d", &t);
    84     while (t --)
    85     {
    86         scanf("%d", &n);
    87         rt = 1;
    88         memset(s, 0, sizeof(s));
    89         for (int i = 0; i < n; ++ i)
    90         {
    91             scanf("%s", ss);
    92             my_insert(ss);
    93         }
    94         scanf("%s", ss);
    95         printf("%d
    ", ac(ss));
    96     }
    97     return 0;
    98 }
  • 相关阅读:
    ffmpeg rtmp推流 视频转码
    java日志发展史 log4j slf4j log4j2 jul jcl 日志和各种桥接包的关系
    nginx stream 流转发,可以转发rtmp、mysql访问流,转发rtmp、jdbc请求
    java web http 转https 通过nginx代理访问
    linux 服务器磁盘挂载
    novnc 通过websockify代理 配置多点访问
    linux 文件服务 minio 安装部署配置
    AOP实现原理,手写aop
    java 泛型
    JAVA反射getGenericSuperclass()用法
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9541372.html
Copyright © 2011-2022 走看看