B. Palindromes Coloring
You have a string ss consisting of lowercase Latin alphabet letters.
You can color some letters in colors from 11 to kk. It is not necessary to paint all the letters. But for each color, there must be a letter painted in that color.
Then you can swap any two symbols painted in the same color as many times as you want.
After that, kk strings will be created, ii-th of them will contain all the characters colored in the color ii, written in the order of their sequence in the string ss.
Your task is to color the characters of the string so that all the resulting kk strings are palindromes, and the length of the shortest of these kk strings is as large as possible.
Read the note for the first test case of the example if you need a clarification.
Recall that a string is a palindrome if it reads the same way both from left to right and from right to left. For example, the strings abacaba, cccc, z and dxd are palindromes, but the strings abab and aaabaa — are not.
Input
The first line of input data contains a single integer tt (1≤t≤1041≤t≤104) — the number of input data sets in the test.
The descriptions of the input data sets follow.
The first line of the description of each input data set contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the length of the string and the number of colors in which its letters can be painted. The second line of the description of each input data set contains a string ss of length nn consisting of lowercase letters of the Latin alphabet.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅1052⋅105.
Output
For each set of input data, output a single integer — the maximum length of the shortest palindrome string that can be obtained.
Example
input
Copy
10 8 2 bxyaxzay 6 3 aaaaaa 6 1 abcdef 6 6 abcdef 3 2 dxd 11 2 abcabcabcac 6 6 sipkic 7 2 eatoohd 3 1 llw 6 2 bfvfbvoutput
Copy
3 2 1 1 1 5 1 1 3 3Note
- In the first test case, ss="bxyaxzay", k=2k=2. We use indices in the string from 11 to 88. The following coloring will work: bxyaxzaybxyaxzay (the letter z remained uncolored). After painting:
- swap two red characters (with the indices 11 and 44), we get axybxzayaxybxzay;
- swap two blue characters (with the indices 55 and 88), we get axybyzaxaxybyzax.
Now, for each of the two colors we write out the corresponding characters from left to right, we get two strings abaaba and xyyxxyyx. Both of them are palindromes, the length of the shortest is 33. It can be shown that the greatest length of the shortest palindrome cannot be achieved.
- In the second set of input data, the following coloring is suitable: [1,1,2,2,3,3][1,1,2,2,3,3]. There is no need to swap characters. Both received strings are equal to aa, they are palindromes and their length is 22.
- In the third set of input data, you can color any character and take it into a string.
- In the fourth set of input data, you can color the iith character in the color ii.
- In the fifth set of input data can be colored in each of the colors of one character.
- In the sixth set of input data, the following coloring is suitable: [1,1,1,1,1,2,2,2,2,2,0][1,1,1,1,1,2,2,2,2,2,0]. Rearrange the characters so as to get the palindromes abcba and acbca.
题目类型:模拟、分配
解题关键:构造平均最长对称序列
1.先分配偶数个字母,再分配奇数个字母,看奇数个字母是不是足够多分给每种颜色。
WA代码:没考虑奇数的分配
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int cnt[26];
bool cmp(int a, int b){ return a > b ;}
int main()
{
int t;
cin>>t;
string s;
while(t -- )
{
int n, k;
cin>>n>>k;
cin>>s;
if(k == n)
{
cout<<1<<endl;
}
else {
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i< n; i++)
{
cnt[s[i] - 'a']++;
}
bool flag = false;
int len = 0;
sort(cnt, cnt+25, cmp);
for(int i=0; i<26 && cnt[i]; i++)
{
if(cnt[i] % 2 == 0 ) len += cnt[i];
else if(!flag && cnt[i]%2 == 1) len += cnt[i], flag = true;
else if(flag && cnt[i]%2 == 1 ) len += (cnt[i]-1);
}
len /= k;
if(len) cout<<len<<endl;
else cout<<1<<endl;
}
}
return 0;
}
AC代码:队友太强了 QAQ
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int cnt[26];
bool cmp(int a, int b){ return a > b ;}
int main()
{
int t;
cin>>t;
string s;
while(t -- )
{
int n, k;
cin>>n>>k;
cin>>s;
if(k == n)
{
cout<<1<<endl;
}
else {
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i< n; i++)
{
cnt[s[i] - 'a']++;
}
int x = 0, y = 0;
for(int i=0; i<26; i++)
{
if(cnt[i] % 2 == 1) y++;
x += cnt[i]/2;
/*if(cnt[i] % 2 == 0 ) len += cnt[i];
else if(!flag && cnt[i]%2 == 1) len += cnt[i], flag = true;
else if(flag && cnt[i]%2 == 1 ) len += (cnt[i]-1);
*/
}
int len = x / k; //y本来就是孤立的奇数的,x - len*k 对称序列中多出来的部分
if((x - len*k )* 2 + y >= k) cout<<2*len+1<<endl;//存在孤立的奇数个数足够多,能给每个对称序列都分一个最终答案+1
else cout<<2*len<<endl;//不够分
}
}
return 0;
}