zoukankan      html  css  js  c++  java
  • hdu -2222 Keywords Search(AC自动机模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222

    求目标串出现了几个模式串.

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cmath>
      4 #include <vector>
      5 #include <cstring>
      6 #include <algorithm>
      7 #include <string>
      8 #include <set>
      9 #include <functional>
     10 #include <numeric>
     11 #include <sstream>
     12 #include <stack>
     13 #include <map>
     14 #include <queue>
     15 
     16 #define CL(arr, val)    memset(arr, val, sizeof(arr))
     17 
     18 #define ll long long
     19 #define inf 0x7f7f7f7f
     20 #define lc l,m,rt<<1
     21 #define rc m + 1,r,rt<<1|1
     22 #define pi acos(-1.0)
     23 
     24 #define L(x)    (x) << 1
     25 #define R(x)    (x) << 1 | 1
     26 #define MID(l, r)   (l + r) >> 1
     27 #define Min(x, y)   (x) < (y) ? (x) : (y)
     28 #define Max(x, y)   (x) < (y) ? (y) : (x)
     29 #define E(x)        (1 << (x))
     30 #define iabs(x)     (x) < 0 ? -(x) : (x)
     31 #define OUT(x)  printf("%I64d
    ", x)
     32 #define lowbit(x)   (x)&(-x)
     33 #define Read()  freopen("din.txt", "r", stdin)
     34 #define Write() freopen("dout.txt", "w", stdout);
     35 
     36 
     37 #define Nn 510007
     38 #define Mc 26
     39 
     40 using namespace std;
     41 
     42 class Acautomaton
     43 {
     44     private:
     45 
     46     int chd[Nn][Mc];
     47     int fail[Nn];
     48     int ID[Mc];
     49     int val[Nn];
     50     int Q[Nn];
     51     int sz;
     52 
     53     public:
     54 
     55     void Init()
     56     {
     57         fail[0] = 0;
     58         for (int i = 0; i < Mc; ++i)
     59         {
     60             ID[i] = i;
     61         }
     62     }
     63     void Reset()
     64     {
     65         CL(chd[0],0);
     66         sz = 1;
     67     }
     68     void insert(char *s,int key)
     69     {
     70         int p = 0;
     71         for (; *s; s++)
     72         {
     73             int k = ID[*s - 'a'];
     74             if (!chd[p][k])
     75             {
     76                 CL(chd[sz],0);
     77                 val[sz] = 0;
     78                 chd[p][k] = sz++;
     79             }
     80             p = chd[p][k];
     81         }
     82         val[p] += key;
     83     }
     84     void Build()
     85     {
     86         int *s = Q ,*e = Q,i;
     87         for (i = 0; i < Mc; ++i)
     88         {
     89             if (chd[0][i])
     90             {
     91                 *e++ = chd[0][i];
     92                 fail[chd[0][i]] = 0;
     93             }
     94         }
     95         while (s != e)
     96         {
     97             int u = *s++;
     98             for (i = 0; i < Mc; ++i)
     99             {
    100                 int &v = chd[u][i];
    101                 if (v)
    102                 {
    103                     *e++ = v;
    104                     fail[v] = chd[fail[u]][i];
    105                 }
    106                 else v = chd[fail[u]][i];
    107             }
    108         }
    109     }
    110     int solve(char *s)
    111     {
    112         int p = 0;
    113         int k,ans = 0;
    114         for (; *s; s++)
    115         {
    116             k = ID[*s - 'a'];
    117             while (!chd[p][k] && p != 0) p = fail[p];
    118 
    119             p = chd[p][k];
    120 
    121             int rt = p;
    122             while (rt != 0 && val[rt] != -1)
    123             {
    124                 ans += val[rt];
    125                 val[rt] = -1;
    126                 rt = fail[rt];
    127             }
    128         }
    129         return ans;
    130     }
    131 }ac;
    132 
    133 char str[57],tr[1000007];
    134 int n;
    135 
    136 int main()
    137 {
    138     int T;
    139     int i;
    140     scanf("%d",&T);
    141     while (T--)
    142     {
    143         ac.Reset(); ac.Init();
    144         scanf("%d",&n);
    145         for (i = 0; i < n; ++i)
    146         {
    147             scanf("%s",str);
    148             ac.insert(str,1);
    149         }
    150         ac.Build();
    151         scanf("%s",tr);
    152         printf("%d
    ",ac.solve(tr));
    153     }
    154     return 0;
    155 }
  • 相关阅读:
    Codeforces Round #649 (Div. 2) D. Ehab's Last Corollary
    Educational Codeforces Round 89 (Rated for Div. 2) E. Two Arrays
    Educational Codeforces Round 89 (Rated for Div. 2) D. Two Divisors
    Codeforces Round #647 (Div. 2) E. Johnny and Grandmaster
    Codeforces Round #647 (Div. 2) F. Johnny and Megan's Necklace
    Codeforces Round #648 (Div. 2) G. Secure Password
    Codeforces Round #646 (Div. 2) F. Rotating Substrings
    C++STL常见用法
    各类学习慕课(不定期更新
    高阶等差数列
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4605549.html
Copyright © 2011-2022 走看看