POJ 3693 Maximum repetition substring (suffix array)
POJ 3693 Maximum repetition substring
Question Link
Given a string, find the string with the most duplicates among its substrings. If there are the same strings, the minimum output lexicographically
Idea: enumerate the length of l and segment the string by l. In this way, the string with the length of l will certainly contain a segment location so that it can be located at each segment location, perform lcp one time later to find the maximum matching length. If the matching length is left, check the remaining number, and then perform lcp one time later. If the matching length is longer, the number of matching times is increased by 1, so that the answer can be saved in the enumeration process.
In this case, the Lexicographic Order is still a problem. This can fully utilize the features of the sa array, from the smallest Lexicographic Order to the largest enumeration, until a matching position appears, the output ends.
Code:
#include
#include
#include using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;const int MAXLEN = 200005;struct Suffix { int s[MAXLEN]; int sa[MAXLEN], t[MAXLEN], t2[MAXLEN], c[MAXLEN], n; int rank[MAXLEN], height[MAXLEN]; int best[MAXLEN][20]; int len; char str[MAXLEN]; int ans[MAXLEN], an; void build_sa(int m) {n++;int i, *x = t, *y = t2;for (i = 0; i < m; i++) c[i] = 0;for (i = 0; i < n; i++) c[x[i] = s[i]]++;for (i = 1; i < m; i++) c[i] += c[i - 1];for (i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;for (int k = 1; k <= n; k <<= 1) { int p = 0; for (i = n - k; i < n; i++) y[p++] = i; for (i = 0; i < n; i++) if (sa[i] >= k) y[p++] = sa[i] - k; for (i = 0; i < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[y[i]]]++; for (i = 0; i < m; i++) c[i] += c[i - 1]; for (i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y); p = 1; x[sa[0]] = 0; for (i = 1; i < n; i++)x[sa[i]] = (y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]) ? p - 1 : p++; if (p >= n) break; m = p;}n--; } void getHeight() {int i, j, k = 0;for (i = 1; i <= n; i++) rank[sa[i]] = i;for (i = 0; i < n; i++) { if (k) k--; int j = sa[rank[i] - 1]; while (s[i + k] == s[j + k]) k++; height[rank[i]] = k;} } void initRMQ() {for (int i = 0; i < n; i++) best[i][0] = height[i + 1];for (int j = 1; (1<
R) swap(L, R);L++;int k = 0;while ((1<<(k + 1)) <= R - L + 1) k++;return min(best[L][k], best[R - (1<
= 0 && tmp % l && lcp(v, v + l) >= tmp) ti++;if (ti > Max) { an = 0; ans[an++] = l; Max = ti;}else if (ti == Max) ans[an++] = l; }}int ans_v, ans_l;for (int i = 1; i <= n; i++) { int flag = 0; for (int j = 0; j < an; j++) {int tmp = ans[j];if (lcp(sa[i], sa[i] + tmp) >= (Max - 1) * tmp) { ans_v = sa[i]; ans_l = Max * tmp; flag = 1;} } if (flag) break;}for (int i = 0; i < ans_l; i++) printf("%c", str[ans_v + i]);printf("\n"); }} gao;int main() { int cas = 0; while(~scanf("%s", gao.str) && gao.str[0] != '#') {printf("Case %d: ", ++cas);gao.solve(); } return 0;}