zoukankan      html  css  js  c++  java
  • HDU2222(AC自动机)

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 56569    Accepted Submission(s): 18500


    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
     
     1 //2016.10.09
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 const int K = 26;
     9 const int N = 500010;
    10 
    11 struct node//AC自动机中的节点
    12 {
    13     node *ch[K], *fail;
    14     int match;
    15     void Clear(){
    16         memset(this, 0, sizeof(node));
    17     }
    18 }*que[N];
    19 
    20 struct AC_automaton
    21 {
    22     node nodes[N], *root, *superRoot, *cur;
    23     node* newNode(){
    24         cur->Clear();
    25         return cur++;
    26     }
    27     void Clear(){//初始化字典树
    28         cur = nodes;
    29         superRoot = newNode();
    30         root = newNode();
    31         root->fail = superRoot;
    32         for(int i = 0; i < K; i++)
    33               superRoot->ch[i] = root;
    34         superRoot->match = -1;
    35     }
    36     void Insert(char *s){//向字典树中插入一个字符串
    37         node *t = root;
    38         for(; *s; s++){
    39             int p = *s-'a';
    40             if(t->ch[p] == NULL)
    41                   t->ch[p] = newNode();
    42             t = t->ch[p];
    43         }
    44         t->match++;
    45     }
    46     void build(){//构建自动机
    47         int p = 0, q = 0;
    48         que[q++] = root;
    49         while(p != q){
    50             node *t = que[p++];
    51             for(int i = 0; i < K; i++){
    52                 if(t->ch[i]){
    53                     t->ch[i]->fail = t->fail->ch[i];
    54                     que[q++] = t->ch[i];
    55                 }else{
    56                     t->ch[i] = t->fail->ch[i];
    57                 }
    58             }
    59         }
    60     }
    61     int run(char* s){//计算str串中模式串出现的次数
    62         int ans = 0;
    63         node *t = root;
    64         for(; *s; s++){
    65             int p = *s-'a';
    66             t = t->ch[p];
    67             for(node *u = t; u->match != -1; u = u->fail){
    68                 ans += u->match;
    69                 u->match = -1;
    70             }
    71         }
    72         return ans;
    73     }
    74 };
    75 
    76 char str[1000005];
    77 AC_automaton ac;
    78 
    79 int main()
    80 {
    81     int n, T;
    82     scanf("%d", &T);
    83     while(T--)
    84     {
    85         scanf("%d", &n);
    86         getchar();
    87         ac.Clear();
    88         for(int i = 0; i < n; i++){
    89             scanf("%s", str);
    90             ac.Insert(str);
    91         }
    92         ac.build();
    93         scanf("%s", str);
    94         printf("%d
    ", ac.run(str));
    95     }
    96 
    97     return 0;
    98 }
     
  • 相关阅读:
    回调函数实例
    Java StringBuffer 和 StringBuilder 类
    excel被保护或者锁定时候按住alt和enter可以输入换行
    ArrayUtils.
    excel中在某一列上的所有单元格的前后增加
    decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)
    正则表达式:Pattern类与Matcher类详解
    Cocos2d-x文本菜单
    msp430在ccsv5下出现的问题总结
    与TCP/IP协议的初次见面(一)
  • 原文地址:https://www.cnblogs.com/Penn000/p/5943622.html
Copyright © 2011-2022 走看看