题目大意:给一行字母,只有字母,最多1000个字母。 求最长回文穿,有多个情况下,输出最前面的那个。
这次终于让我后缀数组不TLE了…… 哇哇哇哇哇! 我就是喜欢用线段树写RMQ……慢就慢,我不管了
#include <cstdio> #include <iostream> #include <cstring> using namespace std; const int max_strlen = 4000 + 10; char text[max_strlen]; int tub[max_strlen], wa[max_strlen], wb[max_strlen], wv[max_strlen]; int R[max_strlen], height[max_strlen], rank[max_strlen]; int totlen, SA[max_strlen]; int textlen; bool 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) { int i, j, p, *x = wa, *y = wb, *t; 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; m = p, j *= 2) { 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], sa[i - 1], j) ? p - 1 : p ++; } } void calheight(int *r, int *sa, int n) { int i, j, k = 0; for (i = 1; i <= n; ++ i) rank[sa[i]] = i; for (i = 0; i != n; height[rank[i ++ ]] = k) for (j = sa[rank[i] - 1], k ? k -- : 0; r[i + k] == r[j + k]; ++ k); } void HZSZ() { totlen = 0; for (int i = 0; i != textlen; ++ i) R[totlen ++] = text[i]; R[totlen ++] = 1; //串分割符号 for (int i = textlen - 1; i >=0 ; -- i) R[totlen ++] = text[i]; R[totlen] = 0; //结束符号 da(R, SA, totlen + 1, 175); calheight(R, SA, totlen); } struct node { node *ls, *rs; int key, L, R; node(int LL, int RR, int KEY, node *LS, node *RS) { ls = LS; rs = RS; L =LL; R = RR; key = KEY; } node() { ls = rs = this; key = L = R = -1; } }root, Tnull, *null = &Tnull; int mt(node &now, int LL, int RR) { if (LL == RR) return now.key = height[LL]; int mid = (LL + RR) / 2; now.ls = new node(LL, mid, 0, null, null); now.rs = new node(mid + 1, RR, 0, null, null); int a = mt(*now.ls, LL, mid); int b = mt(*now.rs, mid + 1, RR); return now.key = min(a, b); } int find(node &now, int LL, int RR) { if (now.L == LL && now.R == RR) return now.key; int mid = (now.L + now.R) / 2; if (RR <= mid) return find(*now.ls, LL, RR); if (mid < LL) return find(*now.rs, LL, RR); int a = find(*now.ls, LL, mid); int b = find(*now.rs, mid + 1, RR); return min(a, b); } void doit() { root = node(0, totlen, 0, null, null); mt(root, 0, totlen); int ans = 1, flag = 1 ,wz = 0; for (int i = 1; i != textlen; ++ i) { int a = rank[i]; int b = rank[totlen - i - 1]; if (a > b) swap(a, b); int tmp = (find(root, a + 1, b) * 2) - 1; if (tmp> ans) { ans = tmp;; flag = 1; wz = i; } a = rank[i]; b = rank[totlen - i]; if (a > b) swap(a, b); tmp = find(root, a + 1, b) * 2; if (tmp > ans) { ans = tmp; flag = 0; wz = i; } } if (flag == 1) //奇数的情况 { int tmp = ans / 2; for (int i = wz - tmp; i < wz + tmp; ++ i) cout<<text[i]; cout<<text[wz + tmp]; }else{ int tmp = ans / 2; for (int i = wz - tmp; i < wz + tmp - 1; ++ i) cout<<text[i]; cout<<text[wz + tmp - 1]; } } int main() { gets(text); textlen = strlen(text); HZSZ(); doit(); return 0; }