• hdu2222 Keywords Search【AC自动机】


    Keywords Search

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


    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 
     

    题意:

    给定n个模式串和一个文本串,统计文本串中模式串的个数。

    思路:

    AC自动机模板。

    多组数据一定要注意初始化!

      1 #include <iostream>
      2 #include <set>
      3 #include <cmath>
      4 #include <stdio.h>
      5 #include <cstring>
      6 #include <algorithm>
      7 #include <vector>
      8 #include <queue>
      9 #include <map>
     10 //#include <bits/stdc++.h>
     11 using namespace std;
     12 typedef long long LL;
     13 #define inf 0x7f7f7f7f
     14 
     15 int t, n;
     16 const int maxn = 5e5 + 5;
     17 const int maxlen = 1e6 + 5;
     18 
     19 struct tree{
     20     int fail;
     21     int son[26];
     22     int ed;
     23 }AC[maxlen];
     24 int tot = 0;
     25 char s[maxlen];
     26 
     27 void build(char s[])
     28 {
     29     int len = strlen(s);
     30     int now = 0;
     31     for(int i = 0; i < len; i++){
     32         if(AC[now].son[s[i] - 'a'] == 0){
     33             AC[now].son[s[i] - 'a'] = ++tot;
     34         }
     35         now = AC[now].son[s[i] - 'a'];
     36     }
     37     AC[now].ed += 1;
     38 }
     39 
     40 void get_fail()
     41 {
     42     queue<int>que;
     43     for(int i = 0; i < 26; i++){
     44         if(AC[0].son[i] != 0){
     45             AC[AC[0].son[i]].fail = 0;
     46             que.push(AC[0].son[i]);
     47         }
     48     }
     49     while(!que.empty()){
     50         int u = que.front();
     51         que.pop();
     52         for(int i = 0; i < 26; i++){
     53             if(AC[u].son[i] != 0){
     54                 AC[AC[u].son[i]].fail = AC[AC[u].fail].son[i];
     55                 que.push(AC[u].son[i]);
     56             }
     57             else{
     58                 AC[u].son[i] = AC[AC[u].fail].son[i];
     59             }
     60         }
     61     }
     62 }
     63 
     64 int AC_query(char s[])
     65 {
     66     int len = strlen(s);
     67     int now = 0, ans = 0;
     68     for(int i = 0; i < len; i++){
     69         now = AC[now].son[s[i] - 'a'];
     70         for(int t = now; t && AC[t].ed != -1; t = AC[t].fail){
     71             ans += AC[t].ed;
     72             AC[t].ed = -1;
     73         }
     74     }
     75     return ans;
     76 }
     77 
     78 int main()
     79 {
     80     scanf("%d", &t);
     81     while(t--){
     82         for(int i = 0; i <= tot; i++){
     83             AC[i].ed = 0;
     84             AC[i].fail = 0;
     85             for(int j = 0; j < 26; j++){
     86                 AC[i].son[j] = 0;
     87             }
     88         }
     89         tot = 0;
     90         scanf("%d", &n);
     91         for(int i = 0; i < n; i++){
     92             scanf("%s", s);
     93             build(s);
     94         }
     95         AC[0].fail = 0;
     96         get_fail();
     97         scanf("%s", s);
     98         printf("%d
    ", AC_query(s));
     99     }
    100     return 0;
    101 }
  • 相关阅读:
    LeetCode:Container With Most Water
    c#编写的基于Socket的异步通信系统
    关于Thread类中三个interrupt方法的研究与学习(转)
    使用svnkit 的相关实例及相关问题汇总
    创业早期,联合创始人如何避免窝里反?(转)
    程序员解决问题的60个策略(转)
    码农和程序员之间的5个关键差异(转)
    LayoutInflater的使用
    2014年中国95家企业入围世界500强,超日本
    How to get the source code of the chromium of the specified revision
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9897438.html
Copyright © 2020-2023  润新知