zoukankan      html  css  js  c++  java
  • HDU-2222 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
     

     
    思路:
    先把keywords插入Trie,处理出失配指针,再用自动机跑模式串,记录匹配次数时要开一个vis数组,防止多次记录同一结点。
     
     
    Code:
      1 #include<iostream>
      2 #include<algorithm>
      3 #include<cstring>
      4 #include<string>
      5 #include<vector>
      6 #include<map>
      7 #include<set>
      8 #include<queue>
      9 char str[1000005], s[55];
     10 
     11 using namespace std;
     12 #define M(a, b) memset(a, b, sizeof(a))
     13 const int maxn = 500000 + 5;
     14 
     15 struct AC_mata {
     16     int f[maxn], last[maxn], val[maxn], ch[maxn][26], sz;
     17     int ans;
     18     bool vis[maxn];
     19 
     20     int idx(char c) {return c - 'a';}
     21 
     22     void init() {
     23         ans = 0;
     24         sz = 0;
     25         M(vis, 0);
     26         M(val, 0); M(f, 0); M(last, 0); M(ch, 0);
     27     }
     28 
     29     void insert(char *s) {
     30         int u = 0, len = strlen(s);
     31         for (int i = 0; i < len; ++i) {
     32             int c = idx(s[i]);
     33             if (!ch[u][c]) ch[u][c] = ++sz;
     34             u = ch[u][c];
     35         }
     36         ++val[u];
     37     }
     38 
     39     void getFail() {
     40         queue<int> q;
     41         f[0] = 0;
     42         for (int i = 0; i < 26; ++i) {
     43             int u = ch[0][i];
     44             if (u) {q.push(u); f[u] = 0; last[u] = 0;}
     45         }
     46         while (!q.empty()) {
     47             int r = q.front(); q.pop();
     48             for (int i = 0; i < 26; ++i) {
     49                 int u = ch[r][i];
     50                 if (!u) {ch[r][i] = ch[f[r]][i]; continue;}
     51                 q.push(u);
     52                 int v = f[r];
     53                 while (v && !ch[v][i]) v = f[v];
     54                 f[u] = ch[v][i];
     55                 last[u] = val[f[u]] ? f[u] : last[f[u]];
     56             }
     57         }
     58     }
     59 
     60     void cnt(int j) {
     61         if (j) {
     62             if (!vis[j]) ans += val[j];
     63             vis[j] = 1;
     64             cnt(last[j]);
     65         }
     66     }
     67 
     68     void find(char *T) {
     69         int j = 0, len = strlen(T);
     70         for (int i = 0; i < len; ++i) {
     71             int c = idx(T[i]);
     72             j = ch[j][c];
     73             if (val[j]) cnt(j);
     74             else if (last[j]) cnt(last[j]);
     75         }
     76     }
     77 
     78 };
     79 
     80 AC_mata ac;
     81 
     82 int main() {
     83     ios::sync_with_stdio(false);
     84     int T, n;
     85     cin >> T;
     86     while (T--) {
     87         ac.init();
     88         cin >> n;
     89         for (int i = 0; i < n; ++i) {
     90             cin >> s;
     91             ac.insert(s);
     92         }
     93         ac.getFail();
     94         cin >> str;
     95         ac.find(str);
     96         cout << ac.ans << endl;
     97     }
     98 
     99     return 0;
    100 }
  • 相关阅读:
    Linux日志清除
    QT vs x64编译
    VC调用QT的UIDLL
    QT UI获得控件ID(HWND)
    安卓黑客工具
    VMware+Windgb+Win7内核驱动调试
    ubuntu 键盘布局修改
    魅族MX3 smart bar处失灵
    把NDK的工具链提取出来单独使用
    arm指令版本
  • 原文地址:https://www.cnblogs.com/robin1998/p/6551613.html
Copyright © 2011-2022 走看看