题目描述
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.
输入格式
The first line of input data contains a single integer tt ( 1 \le t \le 10^41≤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 \le k \le n \le 2 \cdot 10^51≤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 \cdot 10^52⋅105 .
输出格式
For each set of input data, output a single integer — the maximum length of the shortest palindrome string that can be obtained.
题意翻译
多组询问。
将给出的长度为 N 字符串划分成 K 组,使每组字符串均为回文串,且这 K 组字符串中最短的字符串尽可能长。
输入输出样例
输入 #1
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 bfvfbv
输出 #1
3 2 1 1 1 5 1 1 3 3
本篇题解完全是跟着某网友题解做的
其中唯一不太清楚的是:二分问题中的死循环问题,mid=(l+r+1)>>1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,k;
char c[2000010];
bool check(ll x){
int cnt[27],tot=0,temp=x/2;
for(int i=1;i<=26;++i) cnt[i]=0;//cnt数组用来记录每个字母出现次数
for(int i=1;i<=n;++i) cnt[c[i]-'a'+1]++;
for(int i=1;i<=26;++i) tot+=cnt[i]/2;//tot表示总共有多少对相同字母
if(tot<k*temp) return 0;//k*temp表示需要的相同字母对数
if(x%2==0) return 1;//x为偶数的情况
return n>=k*x;//x为奇数的情况
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%lld%lld",&n,&k);
scanf("%s",c+1);
ll l=1,r=n/k,mid;
while(l<r){
mid=(l+r+1)>>1;//防止出现死循环
if(check(mid))
l=mid;
else
r=mid-1;
}
printf("%lld\n",l);
}
return 0;
}