本题共有 t t t 组数据。
给定一个长度为 n n n 的字符串 s s s 和一个整数 k k k, s s s 只包含小写字母,你可以进行若干次操作(可以是零次),具体操作如下:
选取一个 i i i( 1 ≤ i ≤ n − 2 1\le i\le n-2 1≤i≤n−2),交换 a i a_i ai 和 a i + 2 a_{i+2} ai+2
选取一个 i i i( 1 ≤ i ≤ n − k + 1 1\le i\le n-k+1 1≤i≤n−k+1),翻转区间 s [ i , i + k − 1 ] s_{[i,i+k-1]} s[i,i+k−1]。如果 s = s 1 , s 2 , … , s i − 1 , s i , s i + 1 , … , s i + k − 2 , s i + k − 1 , s i + k , … , s n − 1 , s n s=s_1,s_2,\dots,s_{i-1},s_i,s_{i+1},\dots,s_{i+k-2},s_{i+k-1},s_{i+k},\dots,s_{n-1},s_n s=s1,s2,…,si−1,si,si+1,…,si+k−2,si+k−1,si+k,…,sn−1,sn,可将其改为: s = s 1 , s 2 , … , s i − 1 , s i + k − 1 , s i + k − 2 , … , s i + 1 , s i , s i + k , … , s n − 1 , s n s=s_1,s_2,\dots,s_{i-1},s_{i+k-1},s_{i+k-2},\dots,s_{i+1},s_i,s_{i+k},\dots,s_{n-1},s_n s=s1,s2,…,si−1,si+k−1,si+k−2,…,si+1,si,si+k,…,sn−1,sn
输出经过若干次操作后得到的的按字典顺序排列的最小字符串。
第一行包含一个正整数 t t t,具体含义见上。
第二行包含两个正整数 n n n 和 k k k。
接下来一行 包含一个长度为 n n n 的字符串 s s s。
对于每个测试用例,在进行若干次操作后输出按字典顺序排列的最小字符串。
1 ≤ t ≤ 1 0 4 , 1 ≤ k ≤ n ≤ 1 0 5 1\le t\le10^4,1\le k\le n\le10^5 1≤t≤104,1≤k≤n≤105。
Translate by @Moss_spresd
You are given a string $ s $ of length $ n $ consisting of lowercase English letters, and an integer $ k $ . In one step you can perform any one of the two operations below:
You can make as many steps as you want (possibly, zero). Your task is to find the lexicographically smallest string you can obtain after some number of steps.
A string $ a $ is lexicographically smaller than a string $ b $ of the same length if and only if the following holds:
Each test contains multiple test cases. The first line contains the number of test cases $ t $ ( $ 1 \le t \le 10^4 $ ). The description of the test cases follows.
The first line of each test case contains two integers $ n $ and $ k $ ( $ 1 \le k < n \le 10^5 $ ).
The second line of each test case contains the string $ s $ of length $ n $ consisting of lowercase English letters.
It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^5 $ .
For each test case, print the lexicographically smallest string after doing some (possibly, zero) steps.
5
4 2
nima
5 3
panda
9 2
theforces
7 3
amirfar
6 4
rounds
aimn
aandp
ceefhorst
aafmirr
dnorsu
In the first test case, we can obtain the string “aimn” using the following operations:
It can be proven that we cannot obtain any string lexicographically smaller than “aimn”. Therefore, “aimn” is the answer.
In the second test case, we can obtain the string “aandp” using the following operations:
It can be proven that we cannot obtain any string lexicographically smaller than “aandp”. Therefore, “aandp” is the answer.
两种操作后,能得到的字典序排列最小的字符串
观察这两种操作
奇数位可以互换。
此时我们观察第二种操作,选取一个 i i i( 1 ≤ i ≤ n − k + 1 1\le i\le n-k+1 1≤i≤n−k+1),翻转区间 s [ i , i + k − 1 ] , s_{[i,i+k-1]}, s[i,i+k−1],
s = s 1 , s 2 , s i − 1 , s i , s i + 1 , , ˙ s i + k − 2 , s i + k − 1 , s i + k , … , s n − 1 , s n s=s_1,s_2,s_{i-1},s_i,s_{i+1},\dot,s_{i+k-2},s_{i+k-1},s_{i+k},\dots,s_{n-1},s_n s=s1,s2,si−1,si,si+1,,˙si+k−2,si+k−1,si+k,…,sn−1,sn,
s = s 1 , s 2 , s i − 1 , s i + k − 1 , s i + k − 2 , … , s i + 1 , s i , s i + k , … , s n − 1 , s n s=s_1,s_2,s_{i-1},s_{i+k-1},s_{i+k-2},\dots,s_{i+1},s_i,s_{i+k},\dots,s_{n-1},s_n s=s1,s2,si−1,si+k−1,si+k−2,…,si+1,si,si+k,…,sn−1,sn
种操作,我们就可以交换偶数与偶数,奇数与奇数,偶数与奇数,奇数与偶数的
字符了,那么我们直接 $sort\left ( \right ) $ 排序即可。
数,偶数与偶数位置的字符了,所以我们分别针对 n n n 的奇偶性分别排序输出即可
#include
using namespace std;
const int N=1e5+10;
int t;
char s[N]; //存储原始字符
char j[N],o[N]; //分别存储奇数与偶数位的字符
int main()
{
cin>>t;
while(t--)
{
int n=0,k=0;
int l=0,u=0; //分别记录偶数与奇数的数量
cin>>n>>k>>s;
if(k%2==0){
sort(s,s+n);
cout<<s<<endl;
}
else {
if(n%2==0){
for(int i=0;i<n;i++)
{
if((i+1)%2==0)o[l++]=s[i];
else j[u++]=s[i];
}
sort(o,o+l);
sort(j,j+u);
for(int i=0;i<l;i++) cout<<j[i]<<o[i];
cout<<endl;
}
else
{
for(int i=0;i<n;i++)
{
if((i+1)%2==0)o[l++]=s[i];
else j[u++]=s[i];//奇数
}
sort(o,o+l);
sort(j,j+u);
for(int i=0;i<l;i++) cout<<j[i]<<o[i];
cout<<j[u-1];
cout<<endl;
}
}
}
return 0;
}