可算过了
这是道论文题。有相应的论文的。
主要想法是枚举长度,然后跳着遍历。
我这里维护了两个rmq,并且在后面又接了一个倒序的字符串
网上有人说不用这样,只要在往前遍历就好了。但我觉得这是错的,很可能会将复杂度提升到O(n^2)
所以,我维护了两个rmq
累死了,也没快多少
代码如下:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> using namespace std; const int max_n = 2e5 + 100; char s[max_n]; //后缀数组模板. //SA[i] 排名为i的后缀的起始坐标 //rank[i] 起始坐标为i的后缀的排名 //height[i]=k 表示SA[i-1]与SA[i]代表的后缀的最长公共前缀的长度为k int a[max_n]; int ranks[max_n], SA[max_n], height[max_n]; int wa[max_n], wb[max_n], wvarr[max_n], wsarr[max_n]; inline int cmp(int* r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; } inline void get_sa(int* r, int* sa, int n, int m) { //r:原数组 //sa:SA //n:原数组长度 //m:原数组种类数,用于基数排序 ++n; int i, j, p, * x = wa, * y = wb, * t; for (i = 0; i < m; ++i) wsarr[i] = 0; for (i = 0; i < n; ++i) wsarr[x[i] = r[i]]++; for (i = 1; i < m; ++i) wsarr[i] += wsarr[i - 1]; for (i = n - 1; i >= 0; --i) sa[--wsarr[x[i]]] = i; for (j = 1, p = 1; p < n; j <<= 1, 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) wvarr[i] = x[y[i]]; for (i = 0; i < m; ++i) wsarr[i] = 0; for (i = 0; i < n; ++i) wsarr[wvarr[i]]++; for (i = 1; i < m; ++i) wsarr[i] += wsarr[i - 1]; for (i = n - 1; i >= 0; --i) sa[--wsarr[wvarr[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++; } }//求解高度数组,height[i]指排名 void get_height(int* r, int* sa, int n) { int i, j, k = 0; for (i = 1; i <= n; ++i) ranks[sa[i]] = i; for (i = 0; i < n; height[ranks[i++]] = k) for (k ? k-- : 0, j = sa[ranks[i] - 1]; r[i + k] == r[j + k]; k++); return; } //后缀数组 //附加rmq_st 专门对后缀数组 int st[max_n][32]; int st2[max_n][32]; void initSt(int a[], int n) { for (int i = 0;i <= n;++i)st[i][0] = height[i]; int mxk = (int)log2(n + 1); for (int k = 1;k <= mxk;++k) { for (int i = 0;i <= n;++i) { if (i + (1 << k) - 1 > n)break; st[i][k] = min(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]); } } for (int i = 0;i <= n;++i)st2[i][0] = ranks[i]; for (int k = 1;k <= mxk;++k) { for (int i = 0;i <= n;++i) { if (i + (1 << k) - 1 > n)break; st2[i][k] = min(st2[i][k - 1], st2[i + (1 << (k - 1))][k - 1]); } } } int que(int l, int r) { l = ranks[l];r = ranks[r]; if (l > r)swap(l, r); ++l; int len = log2(r - l + 1); return min(st[l][len], st[r - (1 << len) + 1][len]); } int que2(int l,int r) { if (l > r)swap(l, r); int len = log2(r - l + 1); return min(st2[l][len], st2[r - (1 << len) + 1][len]); } int main() { ios::sync_with_stdio(0); int tcase = 0; while (cin >> s) { if (s[0] == '#')break; int n = strlen(s); for (int i = 0;i < n;++i)a[i] = s[i] - 'a' + 1; a[n] = 30;for (int i = n + 1;i < 2 * n + 1;++i)a[i] = a[2 * n - i]; get_sa(a, SA, 2 * n + 1, 40); get_height(a, SA, 2 * n + 1); initSt(a, 2 * n + 1); int Max = 1;int ans = -1;int ll = 0; for (int len = 1;len <= n;++len) { for (int i = 0;i + len <= n;i += len) { if (a[i] != a[i + len])continue; int tmp_len = que(i, i + len); int res = tmp_len / len + 1; int tmp = tmp_len % len; int pos; int k = que(2 * n - i, 2 * n - (i + len)) - 1; if (k >= len - tmp) { ++res; int lft = max(i - k, i - len); int rght = i - len + tmp; pos = SA[que2(lft, rght)]; } else { int lft = i - k; int rght = i; pos = SA[que2(lft, rght)]; }if (res > Max) { Max = res; ans = pos; ll = len; } else if (res == Max && (ans == -1 || ranks[pos] < ranks[ans])) { ans = pos; ll = len; } } } if (Max == 1) { char ch = 'z'; for (int i = 0;i < n;++i)ch = min(ch, s[i]); cout << "Case " << ++tcase << ": " << ch << endl; } else { cout << "Case " << ++tcase << ": "; for (int i = ans;i < ans + ll * Max;++i)cout << s[i]; cout << endl; } } }