zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    AFNetwork 作用和用法详解
    ios 常见错误记录
    UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
    AutoLayout
    矩阵的法式
    极小多项式
    对角化
    线性映射
    线性方程组的解
    特征值和特征向量
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11324266.html
Copyright © 2011-2022 走看看