fork(4) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
  5. #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
  6. #define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
  7. #define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
  8. #define SZ(S) ((int) ((S).size()))
  9.  
  10. #define DEBUG(x) { cout << #x << " = " << x << endl; }
  11. #define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
  12. #define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
  13.  
  14. struct SuffixArray {
  15. string a;
  16. int N, m;
  17. vector<int> SA, LCP, x, y, w, c;
  18.  
  19. SuffixArray(string _a, int m) : a(" " + _a), N(a.length()), m(m),
  20. SA(N), LCP(N), x(N), y(N), w(max(m, N)), c(N) {
  21. a[0] = 0;
  22. DA();
  23. kasaiLCP();
  24. #define REF(X) { rotate(X.begin(), X.begin()+1, X.end()); X.pop_back(); }
  25. REF(SA); REF(LCP);
  26. a = a.substr(1, a.size());
  27. for(int i = 0; i < SA.size(); ++i) --SA[i];
  28. #undef REF
  29. }
  30.  
  31. inline bool cmp (const int a, const int b, const int l) { return (y[a] == y[b] && y[a + l] == y[b + l]); }
  32.  
  33. void Sort() {
  34. for(int i = 0; i < m; ++i) w[i] = 0;
  35. for(int i = 0; i < N; ++i) ++w[x[y[i]]];
  36. for(int i = 0; i < m - 1; ++i) w[i + 1] += w[i];
  37. for(int i = N - 1; i >= 0; --i) SA[--w[x[y[i]]]] = y[i];
  38. }
  39.  
  40. void DA() {
  41. for(int i = 0; i < N; ++i) x[i] = a[i], y[i] = i;
  42. Sort();
  43. for(int i, j = 1, p = 1; p < N; j <<= 1, m = p) {
  44. for(p = 0, i = N - j; i < N; i++) y[p++] = i;
  45. for (int k = 0; k < N; ++k) if (SA[k] >= j) y[p++] = SA[k] - j;
  46. Sort();
  47. for(swap(x, y), p = 1, x[SA[0]] = 0, i = 1; i < N; ++i)
  48. x[SA[i]] = cmp(SA[i - 1], SA[i], j) ? p - 1 : p++;
  49. }
  50. }
  51.  
  52. void kasaiLCP() {
  53. for (int i = 0; i < N; i++) c[SA[i]] = i;
  54. for (int i = 0, j, k = 0; i < N; LCP[c[i++]] = k)
  55. if (c[i] > 0) for (k ? k-- : 0, j = SA[c[i] - 1]; a[i + k] == a[j + k]; k++);
  56. else k = 0;
  57. }
  58. };
  59.  
  60. const int MN = 1000111;
  61. string a[66];
  62. int id[MN];
  63.  
  64. #define TWO(X) (1LL<<(X))
  65. #define CONTAIN(S,X) (S & TWO(X))
  66. #define left left_
  67. #define right right_
  68. #define CT(X) ((X) << 1)
  69. #define CP(X) (CT(X) + 1)
  70.  
  71. int left[MN], right[MN], st[MN], top, x[MN];
  72. long long it[MN * 4];
  73.  
  74. void update(int i, int l, int r, int u, long long val) {
  75. if (u < l || r < u) return ;
  76. if (l == r) {
  77. it[i] |= val;
  78. return ;
  79. }
  80. int mid = (l + r) >> 1;
  81. update(CT(i), l, mid, u, val);
  82. update(CP(i), mid+1, r, u, val);
  83. it[i] = it[CT(i)] | it[CP(i)];
  84. }
  85.  
  86. long long get(int i, int l, int r, int u, int v) {
  87. if (v < l || r < u) return 0;
  88. if (u <= l && r <= v) return it[i];
  89. int mid = (l + r) >> 1;
  90. return get(CT(i), l, mid, u, v) | get(CP(i), mid+1, r, u, v);
  91. }
  92.  
  93. int start[1000111];
  94.  
  95. int main() {
  96. ios :: sync_with_stdio(false); cin.tie(NULL);
  97. cout << (fixed) << setprecision(6);
  98. int n;
  99. while (cin >> n && n) {
  100. memset(id, 0, sizeof id);
  101. string all = "";
  102. memset(start, 0, sizeof start);
  103. FOR(i,1,n) {
  104. int oldLen = all.length();
  105. cin >> a[i];
  106. all += a[i];
  107. all += i;
  108.  
  109. FOR(x,oldLen,SZ(all)-2)
  110. id[x] = i;
  111. start[oldLen] = i;
  112. }
  113. // PR0(id,all.size());
  114. SuffixArray sa(all, 300);
  115. // DEBUG(all);
  116. // PR0(sa.SA, sa.SA.size());
  117. // PR0(sa.LCP, sa.LCP.size());
  118.  
  119. n = all.size();
  120.  
  121. FOR(i,1,n-1) {
  122. x[i] = sa.LCP[i];
  123. }
  124.  
  125. top = 0; st[0] = -1;
  126. REP(i,n) {
  127. while (top && x[st[top]] >= x[i]) --top;
  128. left[i] = st[top] + 1;
  129. st[++top] = i;
  130. }
  131. top = 0; st[0] = n;
  132. FORD(i,n-1,0) {
  133. while (top && x[st[top]] >= x[i]) --top;
  134. right[i] = st[top] - 1;
  135. st[++top] = i;
  136. }
  137. // PR0(left, n);
  138. // PR0(right, n);
  139. memset(it, 0, sizeof it);
  140. REP(i,n) {
  141. int t = id[sa.SA[i]];
  142. if (t) {
  143. update(1, 1, n, i+1, TWO(t-1LL));
  144. }
  145. }
  146.  
  147. set< long long > masks;
  148. REP(i,n) if (id[sa.SA[i]] && sa.LCP[i]) {
  149. int from = left[i], to = right[i] + 1;
  150. long long t = get(1, 1, n, from, to);
  151. masks.insert(t);
  152. }
  153. REP(i,n) if (start[sa.SA[i]]) {
  154. int id = start[sa.SA[i]];
  155. if (sa.LCP[i] >= a[id].length()) continue;
  156. if (i < n-1 && sa.LCP[i+1] >= a[id].length()) continue;
  157.  
  158. masks.insert(TWO(start[sa.SA[i]] - 1));
  159. }
  160. // for(auto mask : masks) cout << mask << ' '; cout << endl;
  161.  
  162. cout << SZ(masks) << endl;
  163. }
  164. return 0;
  165. }
  166.  
  167.  
Success #stdin #stdout 0.04s 58176KB
stdin
6
form
formal
malformed
for
man
remake
3
cool
cool
old
0
stdout
11
3