E - Reversing and Concatenating Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 13001300

問題文

高橋君は英小文字からなる長さ NN の文字列 SS を持っています。 高橋君は SS に対して以下の操作を KK 回行うことにしました。

  • SS を反転した文字列を TT として、SSTT をこの順に連結して得られる文字列を UU とする。
  • ある UU の連続する長さ NN の部分文字列を SS' として、SSSS' で置き換える。

最終的な SS として考えられる文字列の内、辞書順で最小のものを求めてください。

制約

  • 1N50001 ≦ N ≦ 5000
  • 1K1091 ≦ K ≦ 10^9
  • S=N|S|=N
  • SS は英小文字からなる

入力

入力は以下の形式で標準入力から与えられる。

NN KK
SS

出力

最終的な SS として考えられる文字列の内、辞書順で最小のものを出力せよ。


入力例 1Copy

Copy
5 1
bacba

出力例 1Copy

Copy
aabca

S=S=bacbaのとき、T=T=abcab, U=U=bacbaabcabであるので S=S'=aabcaとするのが最適です。


入力例 2Copy

Copy
10 2
bbaabbbaab

出力例 2Copy

Copy
aaaabbaabb

Score : 13001300 points

Problem Statement

Takahashi has a string SS of length NN consisting of lowercase English letters. On this string, he will perform the following operation KK times:

  • Let TT be the string obtained by reversing SS, and UU be the string obtained by concatenating SS and TT in this order.
  • Let SS' be some contiguous substring of UU with length NN, and replace SS with SS'.

Among the strings that can be the string SS after the KK operations, find the lexicographically smallest possible one.

Constraints

  • 1N50001 \leq N \leq 5000
  • 1K1091 \leq K \leq 10^9
  • S=N|S|=N
  • SS consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

NN KK
SS

Output

Print the lexicographically smallest possible string that can be the string SS after the KK operations.


Sample Input 1Copy

Copy
5 1
bacba

Sample Output 1Copy

Copy
aabca

When S=S=bacba, T=T=abcab, U=U=bacbaabcab, and the optimal choice of SS' is S=S'=aabca.


Sample Input 2Copy

Copy
10 2
bbaabbbaab

Sample Output 2Copy

Copy
aaaabbaabb


2025-03-14 (Fri)
02:15:45 +00:00