A. Ela Sorting Books
Ela loves reading a lot, just like her new co-workers in DTL! On her first day after becoming an engineer in DTL, she is challenged by a co-worker to sort a heap of books into different compartments on the shelf. |
nn books must be split into kk compartments on the bookshelf (nn is divisible by kk). Each book is represented by a lowercase Latin letter from 'a' to 'y' inclusively, which is the beginning letter in the title of the book.
Ela must stack exactly nknk books in each compartment. After the books are stacked, for each compartment indexed from 11 to kk, she takes the minimum excluded (MEX) letter of the multiset of letters formed by letters representing all books in that compartment, then combines the resulting letters into a string. The first letter of the resulting string is the MEX letter of the multiset of letters formed by the first compartment, the second letter of the resulting string is the MEX letter of the multiset of letters formed by the second compartment, ... and so on. Please note, under the constraint of this problem, MEX letter can always be determined for any multiset found in this problem because 'z' is not used.
What is the lexicographically greatest resulting string possible that Ela can create?
A string aa is lexicographically greater than a string bb if and only if one of the following holds:
- bb is a prefix of aa, but b≠ab≠a;
- in the first position where aa and bb differ, the string aa has a letter that appears later in the alphabet than the corresponding letter in bb.
The minimum excluded (MEX) letter of a multiset of letters is the letter that appears earliest in the alphabet and is not contained in the multiset. For example, if a multiset of letters contains 77 letters 'b', 'a', 'b', 'c', 'e', 'c', 'f' respectively, then the MEX letter of this compartment is 'd', because 'd' is not included in the multiset, and all letters comes before 'd' in the alphabet, namely 'a', 'b' and 'c', are included in the multiset.
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100). Description of the test cases follows.
The first line of each test case contains two integers nn and kk (1≤n≤2001≤n≤200; 1≤k≤n1≤k≤n). It is guaranteed that nn is divisible by kk.
The second line of each test case contains a string of nn lowercase Latin letters from 'a' to 'y' inclusively. Each letter represents the starting letter of the title of a book in the initial heap.
It is guaranteed that the sum of nn over all test cases does not exceed 10001000.
For each test case, output a string of kk letters which is the most optimal string that Ela can find.
edb
ccbbba
bbbbbaaaaaaa
z
aaaaa
In the first test case, the books can be divided into 33 compartments as below:
- the first compartment contains the books with indices 1,2,3,71,2,3,7: multiset1={multiset1={'c', 'a', 'b', 'd'}} →→ MEX(multiset1)=MEX(multiset1)= 'e'
- the second compartment contains the books with indices 4,5,6,94,5,6,9 : multiset2={multiset2={'c', 'c', 'a', 'b'}} →→ MEX(multiset2)=MEX(multiset2)= 'd'
- the third compartment contains the remaining books 8,10,11,128,10,11,12 : multiset3={multiset3={ 'a', 'a', 'a', 'c'}} →→ MEX(multiset3)=MEX(multiset3)= 'b'
Therefore, the answer is 'edb'. It can be proven that there is no way that Ela can arrange the books so that it results in a lexicographically greater string.
我的代码
#include <bits/stdc++.h>
using namespace std;
int f[30];
int main()
{
int t;
cin >> t;
while (t--)
{
memset(f, 0, sizeof f);
int n, k;
string s;
cin >> n >> k;
cin >> s;
int x = n / k;
for (int i = 0; i < n; i++)
{
f[s[i] - 'a']++;
}
for (int i = 1; i <= k; i++)
{
int cnt = 0;
int j = 0;
int u = -1;
bool pd = true;
while (cnt < x)
{
if (f[j] && pd)
{
f[j]--;
j++;
cnt++;
}
else if (!f[j] && pd)
{
if (u == -1)
u = j;
int e = -2;
for (e = 24; e > u; e--)
{
if (f[e])
{
j = e;
break;
}
}
if (e == u)
pd = false;
}
else
{
int mx = f[0];
int r = 0;
for (int y = 0; y < u; y++)
{
if (f[y] >= mx)
r = y;
}
f[r]--;
cnt++;
}
}
if (u == -1)
u = j;
printf("%c", u + 'a');
}
puts("");
}
return 0;
}
AC代码
#include <bits/stdc++.h>
using namespace std;
int f[30];
int main()
{
int t;
cin >> t;
while (t--)
{
memset(f, 0, sizeof f);
int n, k;
string s;
cin >> n >> k;
cin >> s;
int x = n / k;
for (int i = 0; i < n; i++)
{
f[s[i] - 'a']++;
}
for (int i = 1; i <= k; i++)
{
int cnt = 0;
int j = 0;
int u = -1;
while (cnt < x)
{
if (!f[j])
{
u = j;
break;
}
j++;
cnt++;
}
for(int i = 0; i < j; i++) f[i]--;
if (u == -1)
u = j;
printf("%c", u + 'a');
}
puts("");
}
}