CF1348 C. Phoenix and Distribution
Question
給你n個字符,要求你將其分爲k份,每份個數至少有1個,使得新組合的字符串中字典序最大的字符串的字典序儘可能小,打印這個字符串。
Solution
分類討論
字典序最小,先排序。
- ,答案爲,此時剩餘部分直接放在後面所得解最優。
- ,答案爲
- ,答案爲
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll maxn = 1e6 + 5;
const int N = 1e5 + 5;
void solve(){
string s;
int n,k;
cin>>n>>k>>s;
sort(s.begin(),s.end());
if(s[0]!=s[k-1]){
cout<<s[k-1]<<'\n';
}else{
cout<<s[0];
if(s[k]!=s[n-1]){
for(int i=k;i<n;i++) cout<<s[i];
}else{
for(int i=0;i<(n-1)/k;i++) cout<<s[n-1];
}
cout<<'\n';
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;
while(T--){
solve();
}
return 0;
}