[hdu4436 str2int]后缀自动机SAM(或后缀数组SA)
题意:给n个数字串,求它们的所有不包含前导0的不同子串的值之和
思路:把数字串拼接在一起,构造SAM,然后以每个状态的长度len作为特征值从小到大排序,从前往后处理每个状态,相当于按拓扑序在图上合并计算答案。
#include <bits/stdc++.h> using namespace std; #define X first #define Y second #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define all(a) (a).begin(), (a).end() #define mset(a, x) memset(a, x, sizeof(a)) #define mcpy(a, b) memcpy(a, b, sizeof(b)) #define cas() int T, cas = 0; cin >> T; while (T --) template<typename T>bool umax(T&a, const T&b){return a<b?(a=b,true):false;} template<typename T>bool umin(T&a, const T&b){return b<a?(a=b,true):false;} typedef long long ll; typedef pair<int, int> pii; #ifndef ONLINE_JUDGE #include "local.h" #endif const int mod = 2012; const int N = 2e5; class SAM { public: void init() { memset(node, 0, sizeof(node)); sz = last = 0; node[0].len = 0; node[0].link = -1; sz ++; } void add(char c) { int cur = sz ++; node[cur].len = node[last].len + 1; int p; for (p = last; ~p && !node[p].next[c]; p = node[p].link) { node[p].next[c] = cur; } if (p == -1) node[cur].link = 0; else { int q = node[p].next[c]; if (node[p].len + 1 == node[q].len) node[cur].link = q; else { int clone = sz ++; node[clone] = node[q]; node[clone].len = node[p].len + 1; for (; ~p && node[p].next[c] == q; p = node[p].link) { node[p].next[c] = clone; } node[q].link = node[cur].link = clone; } } last = cur; } int c[N << 1], p[N << 1]; int getAns() { mset(c, 0); for (int i = 0; i < sz; i ++) c[node[i].len] ++; for (int i = 1; i <= sz; i ++) c[i] += c[i - 1]; for (int i = 0; i < sz; i ++) p[-- c[node[i].len]] = i; int ans = 0; node[0].cnt = 1; for (int i = 0; i < sz; i ++) { int cur = p[i]; for (int j = 0; j < 10; j ++) { if (!cur && !j || !node[cur].next[j]) continue; int next = node[cur].next[j]; node[next].sum = (node[next].sum + node[cur].sum * 10 + j * node[cur].cnt) % mod; node[next].cnt = (node[next].cnt + node[cur].cnt) % mod; } ans = (ans + node[cur].sum) % mod; } return ans; } private: const static int SZ = 11; struct State { int len, link; int next[SZ]; int sum, cnt; }; State node[N << 1]; int sz, last; }; SAM sam; char s[123456]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); #endif // ONLINE_JUDGE int n; while (cin >> n) { sam.init(); for (int i = 0; i < n; i ++) { scanf("%s", s); for (int j = 0; s[j]; j ++) { sam.add(s[j] - '0'); } sam.add(10); } cout << sam.getAns() << endl; } return 0; }