[hdu4552]最长公共前缀
题意:给一个串s,求s的每个前缀出现次数之和。
思路:对于一个后缀i,设i和原串的最长公共前缀为k,则当前总共可以产生k个答案。因此原题转化为求所有后缀与原串的最长公共前缀之和。模板题。以下为通过模板。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 double db; 50 typedef long long LL; 51 typedef pair<int, int> pii; 52 typedef multiset<int> msi; 53 typedef set<int> si; 54 typedef vector<int> vi; 55 typedef map<int, int> mii; 56 57 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; 58 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 }; 59 const int maxn = 1e5 + 7; 60 const int md = 1e9 + 7; 61 const int inf = 1e9 + 7; 62 const LL inf_L = 1e18 + 7; 63 const double pi = acos(-1.0); 64 const double eps = 1e-6; 65 66 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 67 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 68 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 69 template<class T>T condition(bool f, T a, T b){return f?a:b;} 70 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 71 int make_id(int x, int y, int n) { return x * n + y; } 72 73 struct SparseTable { 74 int d[maxn][20]; 75 int t[maxn]; 76 int n; 77 void resize(int nn) { n = nn; } 78 void Init_min(int a[]) { 79 rep_up0(i, n) d[i][0] = a[i]; 80 for (int j = 1; (1 << j) <= n; j++) { 81 for (int i = 0; i + (1 << j) - 1 < n; i++) { 82 d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]); 83 } 84 } 85 int p = -1; 86 rep_up1(i, n) { 87 if ((i & (i - 1)) == 0) p++; 88 t[i] = p; 89 } 90 } 91 void Init_max(int a[]) { 92 rep_up0(i, n) d[i][0] = a[i]; 93 for (int j = 1; (1 << j) <= n; j++) { 94 for (int i = 0; i + (1 << j) - 1 < n; i++) { 95 d[i][j] = max(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]); 96 } 97 } 98 int p = -1; 99 rep_up1(i, n) { 100 if ((i & (i - 1) == 0)) p++; 101 t[i] = p; 102 } 103 } 104 int RMQ_min(int L, int R) { 105 int p = t[R - L + 1]; 106 return min(d[L][p], d[R - (1 << p) + 1][p]); 107 } 108 int RMQ_max(int L, int R) { 109 int p = t[R - L + 1]; 110 return max(d[L][p], d[R - (1 << p) + 1][p]); 111 } 112 }; 113 114 /// 构造后缀数组的之前,需要在原串末尾加个空字符(比其它字符都小即可), 115 ///把这个空字符看成原串的一部分(这样在比较的时候到这个位置一定可以分个大小), 116 ///所以n应该为原序列长度+1,后缀n-1是"空串",sa[0]总是n-1。 117 struct SuffixArray { 118 int n; 119 int arr[6][maxn]; 120 int *sa = arr[0], *x = arr[1], *y = arr[2], *c = arr[3], *rnk = arr[4], *height = arr[5]; 121 void resize(int nn) { n = nn; mem0(arr[0]); } 122 void build_sa(int s[], int m) { // m is biger than the max value of char 123 rep_up0(i, m) c[i] = 0; 124 rep_up0(i, n) c[x[i] = s[i]]++; 125 rep_up1(i, m - 1) c[i] += c[i - 1]; 126 rep_down0(i, n) sa[--c[x[i]]] = i; 127 for (int k = 1; k <= n; k <<= 1) { 128 int p = 0; 129 for (int i = n - k; i < n; i++) y[p++] = i; 130 rep_up0(i, n) if (sa[i] >= k) y[p++] = sa[i] - k; 131 rep_up0(i, m) c[i] = 0; 132 rep_up0(i, n) c[x[y[i]]]++; 133 rep_up0(i, m) c[i] += c[i - 1]; 134 rep_down0(i, n) sa[--c[x[y[i]]]] = y[i]; 135 swap(x, y); 136 p = 1; x[sa[0]] = 0; 137 for (int i = 1; i < n; i++) { 138 x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]? p - 1 : p++; 139 } 140 if (p >= n) break; 141 m = p; 142 } 143 } 144 void build_height(int s[]) { 145 int k = 0; 146 rep_up0(i, n) rnk[sa[i]] = i; 147 rep_up0(i, n) { 148 if (k) k--; 149 int j = sa[rnk[i] - 1]; 150 while (s[i + k] == s[j + k]) k++; 151 height[rnk[i]] = k; 152 } 153 } 154 }; 155 156 SparseTable st; 157 SuffixArray sa; 158 char s[maxn]; 159 int ss[maxn]; 160 161 int main() { 162 //freopen("in.txt", "r", stdin); 163 while (~scanf("%s", s)) { 164 int len = strlen(s) + 1; 165 rep_up0(i, len) ss[i] = s[i]; 166 sa.resize(len); 167 sa.build_sa(ss, 128); 168 sa.build_height(ss); 169 st.resize(len); 170 st.Init_min(sa.height); 171 int ans = (len - 1) % 256; 172 for (int i = 1; i < len; i++) { 173 int j = sa.rnk[0], k = sa.rnk[i]; 174 if (j > k) swap(j, k); 175 ans += st.RMQ_min(j + 1, k); 176 ans %= 256; 177 } 178 cout << ans << endl; 179 } 180 return 0; 181 }