Description
Here's a circle sequence S of length n, and you can choose a position and remove the number on it.
After that,you'll get a integer. More Formally,you Choose a number x (1<=x<=n) and then you'll get the integer Rx = sx+1 ... Sns1s2 ..... Sx-1.
The problem is which number x choose would get the k-th smallest Rx.
If there is more than one answer,choose, the one with the smallest X.
Input
First line of all case contains, numbers n and K. (2?≤?k≤?? n?≤?1?000?000).
Next line contains a number of length n. Each position corresponds to a number of 1-9.
Output
Output x on a single line for each case.
Sample Input
10 5
6228814462
10 4
9282777691
Sample Output
6
7
Hint
Source
First the string repeats once, to find the suffix array, repetition is to be able to get the dictionary sequence of the loop string, and then for the position in the first half of the suffix, you can think of the loop string as a prefix, of course, the dictionary sequence is completely unaffected, and then in fact, each RX is to remove the suffix of the first character, So if rank of this suffix is k, one of the prefixes of this suffix is rx, then the position of the removed character is sa[k]-1, and of course the loop string, so if k==1, the last character is removed.
/************************************************************************* > File name:a.cpp > Author:al ex > Mail: [email protected] > Created time:2015 April 19 Sunday 17:07 42 seconds ************************************** **********************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;classsuffixarray{ Public:Static Const intN =2002000;intInit[n];intX[n];intY[n];intRank[n];intSa[n];intHeight[n];intBuc[n];intLog[n];intdp[n][ -];intSizevoidClear () {size =0; }voidInsertintN) {init[size++] = n; }BOOLcmpint(RNintAintBintL) {return(R[a] = = R[b] && r[a + l] = = R[b + L]); }voidGetsa (intM//m is generally the maximum value of +1{Init[size] =0;intL, p, *x = x, *y = y, n = size +1; for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n; ++i) {++buc[x[i] = init[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0; -i) {sa[--buc[x[i]] = i; } for(L =1, p =1; L <= N && p < n; m = p, l *=2) {p =0; for(inti = n-l; I < n; ++i) {y[p++] = i; } for(inti =0; I < n; ++i) {if(Sa[i] >= L) {y[p++] = sa[i]-l; } } for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n; ++i) {++buc[x[y[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0; -i) {sa[--buc[x[y[i]]] = y[i]; }intI for(Swap (x, y), x[sa[0]] =0, p =1, i =1; I < n; ++i) {X[sa[i]] = cmp (y, Sa[i-1], Sa[i], L)? P-1: p++; } } }voidGetHeight () {inth =0, n = size; for(inti =0; I <= N; ++i) {Rank[sa[i]] = i; } height[0] =0; for(inti =0; I < n; ++i) {if(H >0) {--h; }intJ =sa[rank[i]-1]; for(; i + H < n && j + H < n && init[i + h] = = Init[j + h]; ++h); Height[rank[i]-1] = h; } }//preprocessing logarithm of each number for RMQ, constant optimization voidInitlog () {log[0] = -1; for(inti =1; i < N; ++i) {Log[i] = (I & (i-1)) ? Log[i-1]: Log[i-1] +1; } }voidINITRMQ () {initlog ();intn = size;intLimit for(inti =0; I < n; ++i) {dp[i][0] = Height[i]; } for(intj =1; J <= Log[n]; ++J) {limit = (N-(1<< j)); for(inti =0; I <= limit; ++i) {Dp[i][j] = min (Dp[i][j-1], Dp[i + (1<< (J-1))][j-1]); } } }intLCP (intAintb) {intT A = Rank[a]; b = Rank[b];if(A > B) {Swap (A, b); }--b; t = log[b-a +1];returnMin (dp[a][t], dp[b-(1<< T) +1][t]); }voidSolveintK) {intCNT =0; for(inti =1; I <= size; ++i) {if(Sa[i] < size/2) {++cnt;if(cnt = = k) {if(Sa[i] = =0) {printf("%d\n", (Size >>1)); }Else{printf("%d\n", Sa[i]); } Break; }}}}}sa;Charstr[1001000];intMain () {intN, K; while(~scanf("%d%d", &n, &k)) {scanf('%s ', str); Sa.clear (); for(inti =0; I < n; ++i) {Sa.insert (Str[i]-' 0 '); } for(inti =0; I < n; ++i) {Sa.insert (Str[i]-' 0 '); } Sa.getsa ( -); Sa.getheight (); Sa.solve (k); }return 0;}
WHU1564---Circle (suffix array)