codeforces-1348-C Phoenix and Distribution

codeforces-1348-C Phoenix and Distribution  

传送门:https://codeforces.com/contest/1348/problem/C

题意:把一个长度为n的字符串拆成k个部分,尽量让字典序大的那个串的字典序最小,并输出这个串

因为这k个串里不能有空串,所以必然是要放进去一个字母的

先对原字符串按字典序大小排序

如果这个字符串的字典序最小的那个字母的数量小于k,那字典序大的那个串就是s[k]这个字母(从1开始)你把后半部分接到s[k]后面必然没有接到前面字典序小的字母后面优呀

如果这个字符串的字典序最小的那个字母的数量大于k,那字典序大的那个串就是s[1]和s[k+1]到s[n] 因为s[2]到s[k]被分配给了其他的字符串 其他字符串都只含有s[1]这一个字母

如果这个字符串的字典序最小的那个字母的数量等于k,那就要看字典序第二小(b)的那个字母了,如果直到结尾全是字典序第二小的那个字母,那么就把(b)平均分配,输出较长的那一个

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define lowbit(a) ((a)&-(a))
 5 #define clean(a,b) memset(a,b,sizeof(a))
 6 const int mod = 1e9+7;
 7 const int inf=0x3f3f3f3f;
 8 const int maxn = 1e5+10;
 9 int _;
10 /////////////////////////////////////////////////////////////////
11 char s[maxn];
12 int mp[109];
13 int main()
14 {
15     // freopen("in.in","r",stdin);
16     int t;
17     scanf("%d",&t);
18     while(t--)
19     {
20         int l,k;
21         scanf("%d%d",&l,&k);
22         scanf("%s",s+1);
23         sort(s+1,s+1+l);
24         if(s[k]==s[1])
25         {
26             if(s[k+1]==s[l])
27             {
28                 printf("%c",s[1]);
29                 for(int i=1;i<=((l-k-1)/k+1);i++)
30                 {
31                     printf("%c",s[k+1]);
32                 }
33             }
34             else 
35             {
36                 printf("%c",s[1]);
37                 printf("%s",s+k+1);
38             }
39         }
40         else printf("%c",s[k]);
41         printf("
");
42     }
43     return 0;
44 }
原文地址:https://www.cnblogs.com/YangKun-/p/12828678.html