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.

输入格式

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值