D. Palindromes Coloring-Codeforces Round #764 (Div. 3)

D. Palindromes Coloring

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
bfvfbv

output

Copy

3
2
1
1
1
5
1
1
3
3

Note

  • 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.

----------------------------------------------------------------------------------------------------------------

k种颜色必须都使用上,言外之意必须有k段。其次,最短的回文最长,乍一想还是二分,其实对于cf的题目尽量少想二分,多靠近思维。

这里有一个结论,想让一段数的最小值最大,分配方案应该满足尽量平均分配;

还有一个小技巧,回文串字母出现的次数中,偶数次为主,1次的只有一个字母。也就是说,对于每个字母的出现次数我们进行统计对数还有落单个数。 然后把它们分别最大可能均分到k个子段。

先分哪一个都行,以先分对数为例,dui<k时,有一部分得到两个,有一部分一个也没得到,因为k<=n,为例最大化最小值,让k个串长度都等于1即可,避免了出现0的情况;

dui==k时,每个上面都有两个,dan<k的话,还是有2的存在输出2,dan>=k,那就是3了

dui>k时 每个上面都有dui/k个,另外,没有分完的dui%k*2个外加dan个落单的,这些没有分完的对数无论如何也不可能和落单的形成更多对数,因为没有分配完的对数是两两存在,而落单的单个存在,顶多配成3个,也填不满k对,所以每队分1个就够了,需要判断剩下的是不是大于k个再决定是否加1;

# include <stdio.h>
#include<iostream>
# include<cstring>
# include<vector>
# include<algorithm>
using namespace std;

int  ch[26];
int main()
{

    int t;
    cin>>t;

    while(t--)
    {
        int n,k;
        cin>>n>>k;

        getchar();
        string s;

        cin>>s;
        memset(ch,0,sizeof(ch));
        for(int i=0; i<n; i++)
        {
            ch[s[i]-'a']++;

        }
        int dui=0;
        int dan=0;
        for(int i=0; i<26; i++)
        {
            dui+=ch[i]/2;
            dan+=ch[i]%2;
        }


        if(dui<k)
        {

            cout<<1<<'\n';

        }
        else if(dui==k)
        {
            if(dan>=k)
                cout<<3<<'\n';
            else
                cout<<2<<'\n';
        }
        else
        {
            int cnt=dui/k;
            int sheng=dui%k;
            //一个分cnt对,剩下sheng对没分
            if(sheng*2+dan>=k)
                cout<<cnt*2+1<<'\n';
            else
                cout<<cnt*2<<'\n';
        }
    }


    return  0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值