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.

Input:

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

Out:

3
2
1
1
1
5
1
1
3
3

题意:可以任意交换字符的位置,可以分离出形成最小的回文的子字符串最大。

关键思路:找出有几对相同的字符,因为是可以任意交换位置,因此只要是有相同字符的一对,都可以看成是一样的,如:“aaabbbccccd”可以看成“aaaaccccbdf”加的f是随意写的,目的是突出有一个a不配对。

这样配对的数可以随意组合成更长的回文字符串。由于要分出m个字符串,当对数少于m时,一定无法配对,当大于时,组合起来还会有多余的字符,加上之前就没有配对的字符若超过m,则可以在原本的回文上加长度1,如“aa” + "b" == "aba"。

AC代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2e5 + 10;
int a[30];
void solve(){
	memset(a,0,sizeof a);
	int n,m; cin >> n >> m;
	int cnt = 0;
	for(int i = 0;i < n;i++){
		char c; cin >> c;
		a[c - 'a']++;
	} 
	int x = 0;
	for(int i  = 0;i < 26;i++){
		cnt += a[i]/2;
		x += ((a[i] % 2) == 1);
	}
	
	int k = cnt / m;
	if(k == 0) cout << 1 << endl;
	else if((cnt - k * m)*2 + x >= m){
		cout << k*2 + 1 << endl;
	}else{
		cout << k*2 << endl;
	}
}
signed main()
{
    int t; cin >> t;
    while(t--){
    	solve();
	}
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值