[hdu5164]ac自动机

中文题目:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=563&pid=1003

首先贴一下bc的题解:

首先我们考虑m=1的情况。给定两个数组A={a1,a2,,an}B={b1,b2,,bk},问BA中出现了几次。令ci=ai+1ai,1i<n,同样令di=bi+1bi,1i<k,那么上述问题可以转化为cidi的模式匹配问题,这个正确性显然,也没有什么好证明的。于是对于m=1的情况只有用个kmp即可搞定。 现在考虑m>1的情况,我们考虑用ac自动机来做。考虑到字符集并不是普通的数字,而是一个分数,我们不放搞个分数类,然后用map存转移边。用m个模式串(Bob的序列)建好自动机之后,把文本串(Alice的序列)在自动机上跑一遍即可统计出答案。 

 事实上,这个题的精华就在于序列变换,将问题转化为多模板匹配问题。而且由于转化后的序列有二维状态,可以用map映射重新为状态分配一个值(类似离散)。另外由于字符集比较大,所以ac自动机里面需要一点小小的修改,原来的二维数组的第二维需用map,另外需要同时记录所有儿子的字符值。原来的模板只需要小改就可以适应这种情况了。详见代码:

  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 

 49 typedef long long LL;

 50 typedef pair<int, int> pii;

 51 typedef vector<int> vi;

 52 

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

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

 55 const int maxn = 3e4 + 7;

 56 const int md = 10007;

 57 const int inf = 1e9 + 7;

 58 const LL inf_L = 1e18 + 7;

 59 const double pi = acos(-1.0);

 60 const double eps = 1e-6;

 61 

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

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

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

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

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

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

 68 

 69 LL ans;

 70 

 71 struct AhoCorasickAutoMata {

 72     const static int maxn = 250007;

 73     int cc;

 74     map<int, int> ch[maxn];

 75     vector<int> son[maxn];

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

 77 

 78     void clear() { cc = 0; mem0(val); mem0(ch); mem0(last); mem0(f); rep_up0(i, maxn) { son[i].clear(); ch[i].clear(); } }

 79 

 80     void Insert(int s[], int n) {

 81         int pos = 0;

 82         rep_up0(i, n) {

 83             int id = s[i];

 84             if(!ch[pos][id]) {

 85                 ch[pos][id] = ++ cc;

 86                 son[pos].push_back(id);

 87             }

 88             pos = ch[pos][id];

 89         }

 90         val[pos] ++;

 91     }

 92 

 93     void print(int j) {

 94         if (j) {

 95             ans += val[j];

 96             print(last[j]);

 97         }

 98     }

 99 

100     void find(int T[], int n) {

101         int j = 0;

102         rep_up0(i, n) {

103             int c = T[i];

104             while (j && !ch[j][c]) j = f[j];

105             j = ch[j][c];

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

107             else {

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

109             }

110         }

111     }

112 

113     int getFail() {

114         queue<int> q;

115         f[0] = 0;

116         int SZ = son[0].size();

117         rep_up0(i, SZ) {

118             int c = son[0][i];

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

120             q.push(u);

121         }

122         while (!q.empty()) {

123             int r = q.front(), SZ = son[r].size(); q.pop();

124             rep_up0(i, SZ) {

125                 int c = son[r][i];

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

127                 q.push(u);

128                 int v = f[r];

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

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

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

132             }

133         }

134     }

135 };

136 

137 AhoCorasickAutoMata ac;

138 map<pii, int> mp;

139 int pa[100007], pb[100007];

140 int a[100007], b[300007];

141 

142 int main() {

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

144     int T, n, m, tot = 0;

145     cin >> T;

146     while (T--) {

147         ans = 0;

148         ac.clear();

149         mp.clear();

150         cin >> n >> m;

151         rep_up0(i, n) {

152             sint(a[i]);

153         }

154         rep_up0(i, n - 1) {

155             int g = gcd(a[i], a[i + 1]);

156             int ga = a[i] / g, gb = a[i + 1] / g;

157             int &x = mp[make_pair(ga, gb)];

158             if (!x) x = ++ tot;

159             pa[i] = x;

160         }

161 

162         rep_up0(i, m) {

163             int k;

164             sint(k);

165             rep_up0(j, k) {

166                 sint(b[j]);

167             }

168             if (k > n) continue;

169             if (k == 1) {

170                 ans += n;

171                 continue;

172             }

173             rep_up0(j, k - 1) {

174                 int g = gcd(b[j], b[j + 1]);

175                 int ga = b[j] / g, gb = b[j + 1] / g;

176                 int &x = mp[make_pair(ga, gb)];

177                 if (!x) x = ++ tot;

178                 pb[j] = x;

179             }

180             ac.Insert(pb, k - 1);

181         }

182         ac.getFail();

183         ac.find(pa, n - 1);

184         cout << ans << endl;

185     }

186     return 0;

187 }
View Code

 

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