Codeforces 1348 C.Phoenix and Distribution

  • 题意: 把字符串s中的字符分给k个空字符串,要求每个空字符串都有字符,同时最小化k个字符串中字典序的最大值,输出分配后字典序最大的字符
  • 题解: 分类讨论,如果s排序后,s[0]!=s[k-1]答案就是(s[k])(样例3,5), 如果相等且k~n - 1只有一种字符,就要把这种字符依此分给每个字符串,输出最大的(样例1,4). 如果相等但是k~n-1有多种字符,就把k-1后面的所有字符加在s[0]后面(样例2,6)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<vector>
#include<string>
#include<fstream>
using namespace std;
#define rep(i, a, n) for(int i = a; i <= n; ++ i)
#define per(i, a, n) for(int i = n; i >= a; -- i)
typedef long long ll;
const int N = 3e6 + 105;
const int mod = 998244353;
const double Pi = acos(- 1.0);
const int INF = 0x3f3f3f3f;
const int G = 3, Gi = 332748118;
ll qpow(ll a, ll b) { ll res = 1; while(b){ if(b) res = (res * a) % mod; a = (a * a) % mod; b >>= 1;} return res; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
//


int T, n, k;
string s, res;

int main()
{
    scanf("%d",&T);
    while(T --){
        res.clear();
        int flag = 0;   //k - 1~n - 1元素一样吗
        scanf("%d%d",&n,&k);
        cin>>s;
        sort(s.begin(),s.end());
        for(int i = k + 1; i < n; ++ i){
            if(s[i] != s[i - 1]){
                flag = 1;
                break;
            }
        }
        if(s[0] == s[k - 1]){
            res += s[0];
            if(flag){
                for(int i = k; i < n; ++ i)
                    res += s[i];
            }
            else{
                for(int i = 1; i <= (n - 1) / k; ++ i)
                    res += s[k];
            }
        }
        else{
            res += s[k - 1];
        }
        cout<<res<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/A-sc/p/12817165.html