• Keywords Search (AC自动机模版题)


    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
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  3065 2243 2825 3341 3247 
     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <string.h>
     5 #include <stdlib.h>
     6 #include <math.h>
     7 #include <queue>
     8 #include <set>
     9 
    10 #define INF 0x3f3f3f3f
    11 #define pii pair<int,int>
    12 using namespace std;
    13 const int maxn = 1e6+10;
    14 
    15 char buf[maxn];
    16 struct ac_automation{
    17     int next[maxn][26],fail[maxn],end[maxn];
    18     int root,L;
    19     int newnode(){
    20         for (int i=0;i<26;i++)
    21         {
    22             next[L][i] = -1;
    23         }
    24         end[L++] = 0;
    25         return L-1;
    26     }
    27     void init(){
    28         L = 0;
    29         root = newnode();
    30     }
    31     void insert(char buf[]){
    32         int len = strlen(buf);
    33         int now = root;
    34         for (int i=0;i<len;i++){
    35             if (next[now][buf[i]-'a'] == -1)
    36                 next[now][buf[i]-'a'] = newnode();
    37             now = next[now][buf[i]-'a'];
    38         }
    39         end[now]++;
    40     }
    41     void build(){
    42         queue<int > Q;
    43         fail[root] = root;
    44         for (int i=0;i<26;i++)
    45         {
    46             if (next[root][i] == -1)
    47                 next[root][i] = root;
    48             else{
    49                 fail[next[root][i]] = root;
    50                 Q.push(next[root][i]);
    51             }
    52         }
    53         while (!Q.empty()){
    54             int now = Q.front();
    55             Q.pop();
    56             for (int i=0;i<26;i++){
    57                 if (next[now][i] == -1)
    58                     next[now][i] = next[fail[now]][i];
    59                 else{
    60                     fail[next[now][i]] = next[fail[now]][i];
    61                     Q.push(next[now][i]);
    62                 }
    63             }
    64         }
    65     }
    66     int querry(char buf[]){
    67         int len = strlen(buf);
    68         int now = root;
    69         int res = 0;
    70         for (int i=0;i<len;i++){
    71             now = next[now][buf[i]-'a'];
    72             int temp = now;
    73             while (temp!=root){
    74                 res += end[temp];
    75                 end[temp] = 0;
    76                 temp = fail[temp];
    77             }
    78         }
    79         return res;
    80     }
    81 }ac;
    82 
    83 int main(){
    84     int T;
    85     scanf("%d",&T);
    86     while (T--){
    87         int n;
    88         scanf("%d",&n);
    89         ac.init();
    90         for (int i=0;i<n;i++){
    91             scanf("%s",buf);
    92             ac.insert(buf);
    93         }
    94         ac.build();
    95         scanf("%s",buf);
    96         printf("%d
    ",ac.querry(buf));
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    IIS负载均衡Application Request Route详解第二篇:创建与配置Server Farm
    负载均衡原理与实践详解 第一篇(重新整理)
    负载均衡原理剖析与实践第一篇介绍篇
    查询优化器内核剖析第八篇:执行引擎之数据访问操作Seek(上)
    提高ASP.NET性能与可伸缩性的几个个常用方法剖析
    IIS负载均衡Application Request Route详解第三篇:使用ARR进行Http请求的负载均衡(上)
    负载均衡第二篇负载均衡基础知识普及
    查询优化器内核剖析第七篇:执行引擎之数据访问操作Scan
    活在当下:人这一辈子,你总的坚持点什么
    LINQ 的演变及其对 C# 设计的影响
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11324266.html
Copyright © 2020-2023  润新知