后缀数组构造出的height数组,利用RMQ可以解决回文串问题。
后缀数组的构造需要在后面加一个ASCII码很小的东西,回文串中间也加一个特殊字符比如#
aabbaa 构造后就是 aabbaa#aabbaa*
abcd构造后就是 abcd#dcba*
大概就这些笔记了……
然后就是利用height数组的性质来用平衡树解决RMQ。 用为我用倍增法,所以也就用线段树来解决。整体时间复杂度nlogn,不是优秀的算法,但是对于这题而言还可以的。
/* TASK:calfflac LANG:C++ */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int max_n = 40000 + 5; int wa[max_n], wb[max_n], wv[max_n], tub[max_n]; int R[max_n], sa[max_n], height[max_n], rank[max_n]; string origin_string, son_origin_string; int son_reflect_origin[max_n]; inline char uppercase_to_lowercase(char &ch) { if ('A' <= ch && ch <= 'Z') ch -= 'A' - 'a'; return ch; } #define utl(ch) uppercase_to_lowercase(ch) inline bool if_letter(char ch) { if ('a' <= ch && ch <= 'z') return true; return false; } int cmp(int *r, int a, int b, int l) {return r[a] == r[b] && r[a + l] == r[b + l];} void da(int *r, int *sa, int n, int m = 200) { int *x = wa, *y = wb, *t;//类似于先桶排,然后利用桶离散化,然后继续桶排,然后再离散化…… int i, j, p, k = 0; for (i = 0; i != m; ++ i) tub[i] = 0; for (i = 0; i != n; ++ i) ++ tub[x[i] = r[i]]; for (i = 1; i != m; ++ i) tub[i] += tub[i - 1]; for (i = n - 1; i >= 0; i -- ) sa[-- tub[x[i]]] = i; for (j = 1, p = 1; p < n; j *= 2, m = p) { for (p = 0, i = n - j; i < n; i ++ ) y[p ++ ] = i; for (i = 0; i != n; ++ i) if (sa[i] > j) y[p++] = sa[i] -j ; for (i = 0; i != n; ++ i) wv[i] = x[y[i]]; for (i = 0; i != m; ++ i) tub[i] = 0; for (i = 0; i != n; ++ i) tub[wv[i]] ++ ; for (i = 1; i != m; ++ i) tub[i] += tub[i - 1]; for (i = n - 1; i >= 0; -- i) sa[-- tub[wv[i]]] = y[i]; for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i != n; ++ i) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p ++; } for (i = 0; i != n; ++ i) rank[sa[i]] = i; for (i = 0; i != n; height[rank[i++]] = k) for (k?k--:0,j=sa[rank[i]-1]; r[i+k] == r[j + k]; k ++); } struct node { int L,R; int max_num, min_num; node *left_son, *right_son; node(int L, int R) { this -> L = L; this -> R = R; } node() { left_son = NULL; right_son = NULL; max_num = 0x7fffffff; min_num = - 0x7fffffff; } }root; void build_tree(node &root) { if (root.L == root.R) { root.max_num = root.min_num = height[root.L]; return ; } int mid = (root.L + root.R) / 2; root.left_son = new node(root.L, mid); root.right_son = new node(mid + 1, root.R); build_tree(*root.left_son); build_tree(*root.right_son); root.max_num = max(root.left_son -> max_num, root.right_son -> max_num); root.min_num = min(root.left_son -> min_num, root.right_son -> min_num); return; } int find_tree(node &root, int L, int R) { if (root.L == L && root.R == R) return root.min_num; int mid = (root.L + root.R) / 2; if (R <= mid) return find_tree(*root.left_son, L, R); if (mid < L) return find_tree(*root.right_son, L, R); int A = find_tree(*root.left_son, L, mid); int B = find_tree(*root.right_son, mid + 1, R); return min(A, B); } int main() { freopen("calfflac.in", "r", stdin); freopen("calfflac.out", "w", stdout); char ch; while ((ch = getchar()) != EOF) { origin_string += ch; utl(ch); if (if_letter(ch)) { son_origin_string += ch; son_reflect_origin[son_origin_string.length() - 1] = origin_string.length() - 1; } } int T = son_origin_string.length(); son_origin_string += '#'; for (int i = son_origin_string.length() - 2; i >= 0; -- i) son_origin_string += son_origin_string[i]; for (int i = 0; i != son_origin_string.length(); ++ i) R[i] = (int)son_origin_string[i]; son_origin_string += (char)(5); da(R, sa, son_origin_string.length()); root = node(0, son_origin_string.length()); build_tree(root); int ans = -1; int st, ed; for (int i = 0; i != T; ++ i) { int a = rank[i], b = rank[son_origin_string.length() - i - 2]; if (a > b) swap(a, b); ++ a; int tmp = find_tree(root, a, b); int sb = tmp; tmp = tmp * 2 - 1; if (tmp > ans) { ans = tmp; st = i - sb + 1; ed = i + sb - 1; } a = rank[i]; b = rank[son_origin_string.length() - (i - 1) - 2]; if (a > b) swap(a, b); ++a; tmp = find_tree(root, a, b); sb = tmp; tmp *= 2; if (tmp > ans) { ans = tmp; st = i - sb; ed = i + sb - 1; } } cout<<ans<<endl; for (int i = son_reflect_origin[st]; i <= son_reflect_origin[ed]; ++ i) putchar(origin_string[i]); cout<<endl; return 0; }