Portal
If the data is small, greed will be fine.
This string can be flipped back to the back, and then to find the suffix array, the rank array is very simple.
--code
1#include <cstdio>2#include <iostream>3 #defineN 600014 5 intN, len, m = the, sum;6 intBuc[n], x[n], y[n], sa[n], rank[n];7 CharS[n];8 9InlinevoidBuild_sa ()Ten { One intI, K, p; A for(i =0; I < m; i++) Buc[i] =0; - for(i =0; i < Len; i++) Buc[x[i] = s[i]]++; - for(i =1; I < m; i++) Buc[i] + = buc[i-1]; the for(i = len-1; I >=0; i--) Sa[--buc[x[i]] =i; - for(k =1; K <= Len; K <<=1) - { -p =0; + for(i = len-1; I >= len-k; i--) y[p++] =i; - for(i =0; i < Len; i++)if(Sa[i] >= k) y[p++] = Sa[i]-K; + for(i =0; I < m; i++) Buc[i] =0; A for(i =0; i < Len; i++) buc[x[y[i]]]++; at for(i =1; I < m; i++) Buc[i] + = buc[i-1]; - for(i = len-1; I >=0; i--) sa[--buc[x[y[i] []] =Y[i]; - Std::swap (x, y); -p =1, x[sa[0]] =0; - for(i =1; i < Len; i++) -X[sa[i]] = y[sa[i-1] [= Y[sa[i]] && y[sa[i-1] + K] = = Y[sa[i] + K]? P-1: p++; in if(P >= Len) Break; -m =p; to } + } - the intMain () * { $ intI, L, R;Panax Notoginsengscanf"%d", &n); -Len = n *2; the for(i =0; I < n; i++) GetChar (), s[i] = GetChar (), S[len-i-1] =S[i]; + Build_sa (); A for(i =0; i < Len; i++) Rank[sa[i] =i; theL =0, R = N-1; + while(L <=R) - { $ if(Rank[l] <= rank[len-r-1]) Putchar (s[l++]); $ ElsePutchar (s[r--]); - if(++sum = = the) sum =0, Putchar ('\ n'); - } the return 0; -}
View Code
[luoguP2870] [Usaco07dec] Best bull line, Gold top Cow lines, gold (suffix array)