题目如题了啦。论文上的题目刷的还是蛮快的了,自从后缀数组越写越顺……再也不用担心这个那个写错了~~~今晚一定要刷完论文上的题目,这样明天就可以继续去做SPLAY的题目拉~~~
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int max_n = 200000 + 10; int a[max_n]; int wv[max_n], wa[max_n], wb[max_n], tub[max_n]; int sa[max_n], height[max_n], rank[max_n]; int n, k; inline bool cmp(int *r, int a, int b,int l) {return r[a] == r[b] && r[a + l] == r[b + l];} 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 (k ? k -- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; ++ k); } 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, i = 1, x[sa[0]] = 0; i != n; ++ i) x[sa[i]] = cmp(y, sa[i], sa[i - 1], j) ? p - 1: p ++; } } bool check(int len)//len个长度是否科学 { int count = 0; for (int i = 1; i <= n; ++ i) { if (height[i] < len) { count = 0; continue; } count ++; if (count == k) return true; } return false; } void doit() { int L = 2, R = n + 1, mid; --k; while (L + 1 < R) { mid = (L + R) / 2; if (check(mid)) L = mid; else R = mid; } //ans = L [)区间 cout<<L<<endl; } int main() { ios::sync_with_stdio(false); cin >> n>> k; for (int i = 0; i != n; ++ i) cin >> a[i]; a[n] = 0; da(a, sa, n + 1, 200); calheight(a, sa, n); doit(); return 0; }