題目鏈接
題意:給定一個字符串,求出其子串中,重復次數最多的串,如果有相同的,輸出字典序最小的
思路:枚舉長度l,把字符串按l分段,這樣對於長度為l的字符串,肯定會包含一個分段位置,這樣一來就可以在每個分段位置,往後做一次lcp,求出最大匹配長度,然後如果匹配長度有剩余,看剩余多少,就往前多少位置再做一次lcp,如果匹配出來長度更長,匹配次數就加1,這樣就可以枚舉過程中保存下答案了
這樣問題還有字典序的問題,這個完全可以利用sa數組的特性,從字典序最小往大枚舉,直到出現一個符合的位置就輸出結束
代碼:
#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; }