UVA 12206-stammering Aliens
Topic links
Test instructions: Given a sequence, the maximum number of substrings with more than M occurrences and the longest length of the string is obtained.
Idea: suffix array. After making the height array, we can use two points to find
This problem before also written hash of the writing can also, just write suffix array when, made a stupid error, the none output to node still can't find ... This is the second time the brush problem encountered such a joke than wrong, still have to pay attention to.
。
Code:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxlen = 40005; struct Suffix {int S[maxlen]; int Sa[maxlen], T[maxlen], T2[maxlen], C[maxlen], N; int Rank[maxlen], Height[maxlen]; 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;} }} gao;const int N = 40005;int M;char str[n];int judge (int x) {int ans =-1; for (int i = 1; I <= GAO.N; i++) {if (Gao.n-gao.sa[i] < x) Continue;int Max = Gao.sa[i], cnt = 1;while (gao.height [i + 1] >= x && i < GAO.N) {max = max (max, gao.sa[i + 1]); cnt++; i++;} if (CNT >= m) ans = max (ans, max); } return ans; void Solve () {if (judge (1) = =-1) {printf ("none\n"); return; } int L = 1, r = gao.n-m + 2; while (L < r) {int mid = (L + R)/2;if (judge (mid)! =-1) L = mid + 1;else r = Mid; } l--; printf ("%d%d\n", L, Judge (L));} int main () {while (~SCANF ("%d", &m) && m) {scanf ('%s ', str); int len = strlen (str); for (int i = 0; i < Len i++) Gao.s[i] = str[i]-' a ' + 1;gao.s[len] = 0;GAO.N = Len;gao.build_sa; Gao.getheight (); solve (); } return 0;}
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
UVA 12206-stammering Aliens (suffix array)