zoukankan      html  css  js  c++  java
  • HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222


    Keywords Search

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

    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

    题解:

    AC自动机的模板题。模板来自kuangbin, 在此特别鸣谢!

    代码如下:

      1 #include<bits/stdc++.h>
      2 #define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
      3 #define ms(a,b) memset((a),(b),sizeof((a)))
      4 using namespace std;
      5 typedef long long LL;
      6 const double EPS = 1e-6;
      7 const int INF = 2e9;
      8 const LL LNF = 9e18;
      9 const int MOD = 1e9+7;
     10 const int MAXN = 5e5+10;
     11 
     12 struct Trie //封装在一个结构体中
     13 {
     14     int next[MAXN][26], fail[MAXN], end[MAXN];
     15     int root, L;
     16 
     17     int newnode()
     18     {
     19         for(int i = 0; i<26; i++)
     20             next[L][i] = -1;
     21         end[L++] = 0;
     22         return L-1;
     23     }
     24 
     25     void init()
     26     {
     27         L = 0;
     28         root = newnode();
     29     }
     30 
     31     void insert(char buf[])     //往Trie树中插入单词
     32     {
     33         int len = strlen(buf);
     34         int now = root;
     35         for(int i = 0; i<len; i++)
     36         {
     37             if(next[now][buf[i]-'a'] == -1)
     38                 next[now][buf[i]-'a'] = newnode();
     39             now = next[now][buf[i]-'a'];
     40         }
     41         end[now]++;
     42     }
     43 
     44     void build()        //构建fail指针
     45     {
     46         queue<int>Q;
     47         fail[root] = root;
     48         for(int i = 0; i<26; i++)
     49         {
     50             if(next[root][i] == -1)
     51                 next[root][i] = root;
     52             else
     53                 fail[next[root][i]] = root, Q.push(next[root][i]);
     54         }
     55         while(!Q.empty())
     56         {
     57             int now = Q.front();
     58             Q.pop();
     59             for(int i = 0; i<26; i++)
     60             {
     61                 if(next[now][i] == -1)
     62                     next[now][i] = next[fail[now]][i];
     63                 else
     64                     fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
     65             }
     66         }
     67     }
     68 
     69     int query(char buf[])   //将字符串与Trie树进行匹配
     70     {
     71         int len = strlen(buf);
     72         int now = root;
     73         int res = 0;
     74         for(int i = 0; i<len; i++)
     75         {
     76             now = next[now][buf[i]-'a'];
     77             int tmp = now;
     78             while(tmp != root)
     79             {
     80                 res += end[tmp];
     81                 end[tmp] = 0;
     82                 tmp = fail[tmp];
     83 
     84             }
     85         }
     86         return res;
     87     }
     88 };
     89 
     90 Trie ac;
     91 char buf[MAXN<<1];
     92 int main()
     93 {
     94     int T, n;
     95     scanf("%d",&T);
     96     while(T--)
     97     {
     98         ac.init();
     99         scanf("%d", &n);
    100         for(int i = 1; i<=n; i++)
    101         {
    102             scanf("%s",buf);
    103             ac.insert(buf);
    104         }
    105         ac.build();
    106         scanf("%s",buf);
    107         printf("%d
    ",ac.query(buf));
    108     }
    109     return 0;
    110 }
    View Code
  • 相关阅读:
    Fox Dividing Cheese [CF-371B]
    2021计算机专业方向志愿怎么填?哪一个更香?
    【每天一个编程小技巧】C++ return:使函数立即结束!
    我开发了一个女朋友陪聊系统!【全天24小时推送问候,自动回复女友的微信消息】
    【C++框架编程】Qt 的 信号与槽 简单了解!
    程序人生:一流靠数学,二流靠算法!程序员的数学需要很厉害吗?
    【硬核知识】C语言文件操作!文件的打开和关闭!
    程序员真的已经没救了吗?这可能就是前端鄙视后端的真正原因吧!
    刷题 678 天的感受!Coding使我快乐,bug使我憔悴!
    MQ面试题
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538621.html
Copyright © 2011-2022 走看看