折腾了最长回文串,和模式串的匹配问题。
发现程序慢如狗!
POJ 3461 TLE
#include <cstdio> #include <cstring> const int max_strlen = 1200000 + 10; char pattern[max_strlen], text[max_strlen]; int sa[max_strlen], 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 plen, tlen; 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 != plen; ++ i) R[totlen ++] = pattern[i]; R[totlen ++] = 4; //串分割符号 for (int i = 0; i != tlen; ++ i) R[totlen ++] = text[i]; R[totlen] = 3; //结束符号 da(R, SA, totlen + 1, 175); calheight(R, SA, totlen); } void doit() { int pos = rank[0]; int ans = 0; for (int i = pos; i >= 1; -- i) { if (height[i] < plen) break; ++ ans; } for (int i = pos + 1; i <= totlen; ++ i) { if (height[i] < plen) break; ++ ans; } printf("%d\n", ans); } int main() { int n; scanf("%d\n", &n); while (n --) { gets(pattern); gets(text); plen = strlen(pattern); tlen = strlen(text); HZSZ(); //cal出SA和height数组 doit(); } return 0; }
POJ 3974,直接无法承受后缀数组所需要的内存空间,直接爆内存!
#include <cstdio> #include <iostream> #include <cstring> using namespace std; const int max_strlen = 4000000 + 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; R[totlen ++] = 5;//开头符号 R[totlen ++] = 6; for (int i = 0; i != textlen; ++ i) { R[totlen ++] = text[i]; R[totlen ++] = 6; //字符中断符 } R[totlen ++] = 4; //串分割符号 R[totlen ++] = 6; for (int i = textlen - 1; i >=0 ; -- i) { R[totlen ++] = text[i]; R[totlen ++] = 6; } R[totlen] = 3; //结束符号 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; void mt(node &now, int LL, int RR) { if (LL == RR) { now.key = height[LL]; return; } int mid = (LL + RR) / 2; if (now.ls == null) now.ls = new node(LL, mid, 0, null, null); else { now.ls -> L = LL; now.ls -> R = mid; } if (now.rs == null) now.rs = new node(mid + 1, RR, 0, null, null); else{ now.rs -> L = mid + 1; now.rs -> R = RR; } mt(*now.ls, LL, mid); mt(*now.rs, mid + 1, RR); int a =now.ls -> key; int b = now.rs -> key; now.key = min(a, b); return; } 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 = 0; for (int i = 2; i <= 2 * textlen; ++ i) { int a = rank[i]; int b = rank[totlen - i]; if (a > b) swap(a, b); int tmp = find(root, a + 1, b) - 1; ans = max(ans, tmp ); } cout<<ans<<endl; } int main() { int count=0; while (1) { ++ count; gets(text); if (text[0] == 'E') break; cout<<"Case "<<count<<": "; textlen = strlen(text); HZSZ(); doit(); } return 0; }