[hdu2222]ac自动机(模板)

题意:一个文本串+多个模板串的匹配问题

思路:裸的ac自动机。

  1 #pragma comment(linker, "/STACK:10240000,10240000")

  2 

  3 #include <iostream>

  4 #include <cstdio>

  5 #include <algorithm>

  6 #include <cstdlib>

  7 #include <cstring>

  8 #include <map>

  9 #include <queue>

 10 #include <deque>

 11 #include <cmath>

 12 #include <vector>

 13 #include <ctime>

 14 #include <cctype>

 15 #include <set>

 16 #include <bitset>

 17 #include <functional>

 18 #include <numeric>

 19 #include <stdexcept>

 20 #include <utility>

 21 

 22 using namespace std;

 23 

 24 #define mem0(a) memset(a, 0, sizeof(a))

 25 #define mem_1(a) memset(a, -1, sizeof(a))

 26 #define lson l, m, rt << 1

 27 #define rson m + 1, r, rt << 1 | 1

 28 #define define_m int m = (l + r) >> 1

 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)

 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)

 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)

 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)

 33 #define all(a) (a).begin(), (a).end()

 34 #define lowbit(x) ((x) & (-(x)))

 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}

 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}

 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}

 38 #define pchr(a) putchar(a)

 39 #define pstr(a) printf("%s", a)

 40 #define sstr(a) scanf("%s", a)

 41 #define sint(a) scanf("%d", &a)

 42 #define sint2(a, b) scanf("%d%d", &a, &b)

 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)

 44 #define pint(a) printf("%d\n", a)

 45 #define test_print1(a) cout << "var1 = " << a << endl

 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl

 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl

 48 #define mp(a, b) make_pair(a, b)

 49 #define pb(a) push_back(a)

 50 

 51 typedef long long LL;

 52 typedef pair<int, int> pii;

 53 typedef vector<int> vi;

 54 

 55 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};

 56 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };

 57 const int maxn = 3e4 + 7;

 58 const int md = 10007;

 59 const int inf = 1e9 + 7;

 60 const LL inf_L = 1e18 + 7;

 61 const double pi = acos(-1.0);

 62 const double eps = 1e-6;

 63 

 64 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}

 65 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}

 66 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}

 67 template<class T>T condition(bool f, T a, T b){return f?a:b;}

 68 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}

 69 int make_id(int x, int y, int n) { return x * n + y; }

 70 

 71 int ans;

 72 

 73 struct AhoCorasickAutoMata {

 74     const static int SIGMA_SIZE = 26;

 75     const static int maxn = 250007;

 76     int cc;

 77     int ch[maxn][SIGMA_SIZE];

 78     int val[maxn], last[maxn], f[maxn];

 79 

 80     void clear() { cc = 0; mem0(val); mem0(ch); mem0(last); mem0(f); }

 81 

 82     int idex(char ch) { return ch - 'a'; }

 83 

 84     void Insert(char s[]) {

 85         int pos = 0;

 86         for(int i = 0; s[i]; i++) {

 87             int id = idex(s[i]);

 88             if(!ch[pos][id]) ch[pos][id] = ++ cc;

 89             pos = ch[pos][id];

 90         }

 91         val[pos] ++;

 92     }

 93 

 94     void print(int j) {

 95         if (j) {

 96             ans += val[j];

 97             val[j] = 0;//为了避免重复计数,需要在统计后置零

 98             print(last[j]);

 99         }

100     }

101 

102     void find(char T[]) {

103         int n = strlen(T);

104         int j = 0;

105         rep_up0(i, n) {

106             int c = idex(T[i]);

107             j = ch[j][c];

108             if (val[j]) print(j);

109             else {

110                 if (last[j]) print(last[j]);

111             }

112         }

113     }

114 

115     int getFail() {

116         queue<int> q;

117         f[0] = 0;

118         rep_up0(c, SIGMA_SIZE) {

119             int u = ch[0][c];

120             if (u) {

121                 f[u] = 0;

122                 q.push(u);

123                 last[u] = 0;

124             }

125         }

126         while (!q.empty()) {

127             int r = q.front(); q.pop();

128             rep_up0(c, SIGMA_SIZE) {

129                 int u = ch[r][c];

130                 if (!u) {

131                     ch[r][c] = ch[f[r]][c];

132                     continue;

133                 }

134                 q.push(u);

135                 int v = f[r];

136                 while (v && !ch[v][c]) v = f[v];

137                 f[u] = ch[v][c];

138                 last[u] = val[f[u]]? f[u] : last[f[u]];

139             }

140         }

141     }

142 };

143 

144 AhoCorasickAutoMata ac;

145 char str[55];

146 char s[1000007];

147 

148 int main() {

149     //freopen("in.txt", "r", stdin);

150     int T, n;

151     cin >> T;

152     while (T--) {

153         ac.clear();

154         cin >> n;

155         rep_up0(i, n) {

156             scanf("%s", str);

157             ac.Insert(str);

158         }

159         scanf("%s", s);

160         ac.getFail();

161         ans = 0;

162         ac.find(s);

163         cout << ans << endl;

164     }

165     return 0;

166 }
View Code

 

你可能感兴趣的:(AC自动机)