2617.Cutting the Line

Time Limit: 1s Memory Limit: 256MB

You are given a non-empty line s and an integer k. The following operation is performed with this line exactly once:
A line is split into at most k non-empty substrings, i.e. string s is represented as a concatenation of a set of strings s=t1+t2+...+tm, 1 \le m \le k. Some of strings ti are replaced by strings tir, that is, their record from right to left. The lines are concatenated back in the same order, we get string s'=t'1t'2... t'm, where t'i equals ti or tir.
Your task is to determine the lexicographically smallest string that could be the result of applying the given operation to the string s.

Input Format(From the terminal/stdin)

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

Output Format(To the terminal/stdout)

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

Sample Input 1

Copy
aba
2
   \n
 \n

Sample Output 1

Copy
aab
   \n

Sample Input 2

Copy
aaaabacaba
2
          \n
 \n

Sample Output 2

Copy
aaaaabacab
          \n

Sample Input 3

Copy
bababa
1
      \n
 \n

Sample Output 3

Copy
ababab
      \n

Sample Input 4

Copy
abacabadabacaba
4
               \n
 \n

Sample Output 4

Copy
aababacabacabad
               \n

Submit

请先 登录

© 2025 FAQs