#x1052. CF594E Cutting the Line

    ID: 6066 远端评测题 2000ms 256MiB 尝试: 1 已通过: 1 难度: 10 上传者: 标签>字符串扩展KMPborder相关最小表示法*3100

CF594E Cutting the Line

Cutting the Line

题面翻译

  • 给定一个字符串 ss 和一个正整数 kk
  • 你可以将 ss 分成至多 kk 段,并将每一段翻转或者不翻转。
  • 求最终能够得到的字典序最小的 ss
  • s5×106|s| \le 5 \times 10^6

题目描述

You are given a non-empty line s s and an integer k k . The following operation is performed with this line exactly once:

  • A line is split into at most k k non-empty substrings, i.e. string s s is represented as a concatenation of a set of strings s=t1+t2+...+tm s=t_{1}+t_{2}+...+t_{m} , 1<=m<=k 1<=m<=k .
  • Some of strings ti t_{i} are replaced by strings tir t_{i}^{r} , that is, their record from right to left.
  • The lines are concatenated back in the same order, we get string s=t1t2... tm s'=t'_{1}t'_{2}...\ t'_{m} , where ti t'_{i} equals ti t_{i} or tir t_{i}^{r} .

Your task is to determine the lexicographically smallest string that could be the result of applying the given operation to the string s s .

输入格式

The first line of the input contains string s s ( 1<=s<=5000000 1<=|s|<=5000000 ), consisting of lowercase English letters. The second line contains integer k k ( 1<=k<=s 1<=k<=|s| ) — the maximum number of parts in the partition.

输出格式

In the single line print the lexicographically minimum string s s' which can be obtained as a result of performing the described operation.

样例 #1

样例输入 #1

aba
2

样例输出 #1

aab

样例 #2

样例输入 #2

aaaabacaba
2

样例输出 #2

aaaaabacab

样例 #3

样例输入 #3

bababa
1

样例输出 #3

ababab

样例 #4

样例输入 #4

abacabadabacaba
4

样例输出 #4

aababacabacabad