Test instructions
Given a string $S $.
In some way, the string $S $ into no more than $K $ segment $S _1, S_2, ..., s_k$.
Each $S _i$ has the largest substring $C _i$ in the dictionary order.
Minimizes the maximum value $C _i$.
$N \le 200000$.
Analysis
Through the suffix array, the first dichotomy suffix, and then the two-minute length, the realization of the two-point all the strings.
The decision can be greedy, using the information of the suffix array, record v[i] means position I cannot be with position v[i] in the same paragraph.
Realize
#include <cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<algorithm>using namespacestd;#defineF (I, A, b) for (register int i = (a); I <= (b); i++)#defineP (I, A, b) for (register int i = (a); I >= (b); i--)Const intN =100005;intK, N;CharS[n];intSa[n], rk[n], h[n], tsa[n], trk[n], sum[n];intV[n];voidPrework (void) { intm = -; F (i,1, N) sum[s[i]]++; F (i,1, m) sum[i] + = sum[i-1]; P (i, N,1) sa[sum[s[i]]--] =i; rk[sa[1]] = m =1; F (i,2, N) rk[sa[i]] = (S[sa[i]]! = s[sa[i-1]] ? ++m:m); for(intj =1; M! = N; J <<=1) { intp =0; F (i, N-j+1, n) tsa[++p] =i; F (i,1, N)if(Sa[i] > J) tsa[++p] = sa[i]-J; memset (SUM,0,sizeofsum); F (i,1, N) sum[rk[i]]++; F (i,1, m) sum[i] + = sum[i-1]; P (i, N,1) sa[sum[rk[tsa[i]]]--] =Tsa[i]; memcpy (TRK, RK,sizeofRK); rk[sa[1]] = m =1; F (i,2, N) rk[sa[i]]= ((Trk[sa[i]]! = trk[sa[i-1]] || TRK[SA[I]+J]! = trk[sa[i-1]+J])? ++m:m); } m=0; F (i,1, N) { if(M >0) m--; while(s[sa[rk[i]-1]+m] = = S[i+m]) m++; H[rk[i]]=m; }}inlineBOOLCheck (intM) {intH = N, cnt =1; F (i, M+1, N) {H=min (H, h[i]); V[sa[i]]= sa[i]+i; if(!h[i]) {cnt = k +1; Break; } } for(inti =1, go = n+1; I <= n && cnt <= K; i++) { if(go = i) cnt++, go = n+1; if(V[i] >0) go =min (go, v[i]); } F (i, M+1, N) v[sa[i]] =0; returnCNT <=K;} InlineBOOLCintXintM) {intH = M, cnt =1; V[SA[X]] = sa[x]+l; F (i, X+1, N) {H=min (H, h[i]); V[sa[i]]= sa[i]+i; if(!h[i]) {cnt = k +1; Break; } } for(inti =1, go = n+1; I <= n && cnt <= K; i++) { if(go = i) cnt++, go = n+1; if(V[i] >0) go =min (go, v[i]); } F (i, x, N) v[sa[i]]=0; returnCNT <=K;}intMainvoid) {#ifndef Online_judge freopen ("rabbit.in","R", stdin); #endifscanf ("%d%s", &k, s+1); N= strlen (s+1); Prework (); intL =1, R =N; while(L <R) {intM = (l+r) >>1; Check (M)? R = M:l = m+1; } intx = L; L = h[x]+1, R = n-sa[x]+1; while(L <R) {intM = (l+r) >>1; C (x, M)? R = M:l = m+1; } F (I, sa[x], sa[x]+l-1) Putchar (S[i]); Puts (""); return 0;}
[Xsy 1516] The rabbit's string suffix array