Rabbit's string
Time Limit: 40000/20000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 288 accepted submission (s): 108
Problem descriptionlong long ago, there lived a lot of rabbits in the forest. One day, the king of the rabbit kingdom got a mysterious string and he wanted to study this string.
At first, he wocould divide this string into no more than K substrings. then for each substring s, he looked at all substrings of S, and selected the one which has the largest dictionary order. among those substrings selected in the second round, the king then choose one which has the largest dictionary order, and name it as a "magic string ".
Now he wanted to figure out how to divide the string so that the dictionary order of that "magic string" is as small as possible.
Inputthere are at most 36 test cases.
For each test case, the first line contains a integer k indicating the maximum number of substrings the King coshould divide, and the second line is the original mysterious string which consisted of only lower letters.
The length of the mysterious string is between 1 and 105 and K is between 1 and the length of the mysterious string, inclusive.
The input ends by K = 0.
Outputfor each test case, output the magic string.
Sample Input
3bbaa2ababa0
Sample output
bbaHint For the first test case, the king may divide the string into "b", "b" and "aa". For the second test case, the king may divide the string into "aba" and "ba".
Source2014 ACM/ICPC Asia Regional Guangzhou online
Recommendhujie | we have carefully selected several similar problems for you: 5053 5052 5051 5050 question: give you a string of no more than 1 E5. You can cut it into k consecutive substrings at most. Then, extract the largest lexicographically ordered substring (the substring) from the substring ). Then, the most important sub-string in the Lexicographic Order is called the magic string. Now you need to output the smallest magic string in the Lexicographic Order. Train of Thought: the largest and smallest. It is easy to think of binary (now conditioned reflection ). First, we can find SA and height. Then we can find out how many different substrings are in the suffix of F [I], that is, the top I. Then we rank two magic strings. Through ranking, we can use the f array to locate the ranking POS of the suffix of the magic string and the length Len of the magic string. The solution is to determine whether the solution is feasible. If TP = LCP (Pos, x) = 0 is a suffix with a ranking greater than POs, there is no solution. No matter how it is cut, it is useless. Otherwise we can switch a knife at [SA [X], sa [x] + TP-1. All suffixes after POs are marked once. And then greedy to cut. Check the minimum number of knives. Then you can determine. For details, see the code:
# Include <algorithm> # include <iostream> # include <string. h> # include <stdio. h> using namespace STD; const int INF = 0x3f3f3f3f; const int maxn = 100010; typedef long ll; char TXT [maxn]; int SA [maxn], T1 [maxn], t2 [maxn], CT [maxn], he [maxn], rk [maxn], n, m, cut; int MK [maxn]; ll f [maxn], ans; void getsa (char * st) {int I, K, P, * x = T1, * Y = t2; for (I = 0; I <m; I ++) CT [I] = 0; for (I = 0; I <n; I ++) CT [x [I] = sT [I] ++; for (I = 1; I <m; I ++) CT [I] + = CT [I-1]; for (I = n-1; I> = 0; I --) SA [-- CT [x [I] = I; for (k = 1, P = 1; P <n; k <= 1, m = P) {for (P = 0, 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 ++) CT [I] = 0; for (I = 0; I <n; I ++) CT [x [Y [I] ++; for (I = 1; I <m; I ++) CT [I] + = CT [I-1]; for (I = n-1; I> = 0; I --) sa [-- CT [x [Y [I] = Y [I]; for (SWAp (x, y), P = 1, X [SA [0] = 0, 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 ++ ;}} void gethe (char * st) {int I, j, k = 0; for (I = 0; I <n; I ++) rk [SA [I] = I; for (I = 0; I <n-1; I ++) {If (k) k --; j = sa [rk [I]-1]; while (ST [I + k] = sT [J + k]) K ++; he [rk [I] = K ;}} bool isok (ll p) {int POs, Len, I, PP, CNT; Pos = lower_bound (F + 1, F + 1 + N, P)-f; // locate SA Len = He [POS] + P-f [pos-1]; // determine the string length for (I = 0; I <n; I ++) MK [I] =-1; if (n-sa [POS]> Len) // check whether the suffix of your own to cut MK [SA [POS] = sa [POS] + len-1; for (I = POS + 1; I <= N; I ++) {If (he [I] = 0) retur N false; Len = min (Len, he [I]); // lcp mk [SA [I] = sa [I] + len-1; // sorting is bigger than POs and must be separated.} Pp = N, CNT = 0; for (I = 0; I <n; I ++) {If (MK [I]! =-1) // it is impossible to cut the image together with the backend. Greedy thoughts. Pp = min (PP, MK [I]); If (Pp = I) {CNT ++; If (CNT> cut) return false; pp = n ;}} return CNT <cut; // The number of CNT switches is CNT + 1 .} Int main () {int I, POs, Len; ll low, hi, mid; while (scanf ("% d", & cut), cut) {scanf ("% s", txt); n = strlen (txt) + 1; M = 128; getsa (txt); gethe (txt); n --; f [1] = N-sa [1]; for (I = 2; I <= N; I ++) f [I] = f [I-1] + N-sa [I]-He [I]; Low = 1, HI = f [N], ANS = 1; while (low <= Hi) {mid = (low + hi)> 1; if (isok (MID) ans = mid, HI = mid-1; else low = Mid + 1;} Pos = lower_bound (F + 1, F + 1 + N, ANS)-F; len = He [POS] + ANS-f [pos-1]; TXT [SA [POS] + Len] = 0; printf ("% s \ n ", TXT + SA [POS]);} return 0 ;}
HDU 5030 Rabbit's string (suffix array & Binary)