[Codeforces Round #638 (Div. 2)]C. Phoenix and Distribution

https://codeforces.com/contest/1348/problem/C
题意:把字符串s分成t个非空子串,使字典序最大的最小

首先把s排序,结果是”aaabbdddxz“形式
如果t=1,直接返回排好的s
如果s第t位和第1位不同,答案就是s[t],如t=4,可以分成a…,a…,a…,b把剩下的东西分给a…就好了
如果相同,有两种情况,剩下的字母全一样,那么平分,如aabbbb分成abb,abb
只要有一个不一样,如aabbbbc,分成a,abbbbc,这个b或者c分给前面的a都不合适,因为会把c位置提前字典序变大

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
 
char s[100010];
//vector<>
 
int main() {
    int T;
	cin >> T;
	while(T--) {
		int n,t;
		cin >> n >> t;
		cin >> s+1;
		sort(s+1,s+n+1);
        if (t == 1) {
        	cout << s+1 << "\n";
        	continue;
		}
		if (s[t] != s[1]) cout << s[t] << "\n";
		else {
			/*if (s[n] == 'a') {
				int cnt = n/t;
				if (n%t) cnt++;
				for (int j = 1; j <= cnt; j++)
				  cout << 'a';
				puts("");
			} else */if(s[t+1] == s[n]) {
				cout << s[t];
				int cnt = (n-t)/t;
				if ((n-t)%t) cnt++;
				while(cnt--) cout<<s[t+1];
				puts("");
			} else {
				cout << s+t << "\n";
			}
		}
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33831360/article/details/105885808