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

https://codeforces.com/contest/1624/problem/D

你有一个由小写拉丁字母组成的字符串s。

可以用从1到k的颜色给一些字母上色,**没必要把所有字母都涂上**。但是对于每种颜色,都必须有一个字母用那种颜色画出来。

然后你可以任意多次交换任何两个涂有相同颜色的符号。

之后,将创建k个字符串,其中第i个将包含所有用颜色i着色的字符,按照它们在字符串s中的顺序书写。

你的任务是给字符串的字符着色,使产生的k个字符串都是回文,**并且这k个字符串中最短的长度尽可能大**。

输出
对于每组输入数据,输出一个整数——可以获得的最短回文字符串的最大长度。
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
output
3
2
1
1
1
5
1
1
3
3
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=200200,M=2002;
int a[N],b[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    cin>>T;
    while(T--)
    {
        int n,k;
        cin>>n>>k;
        string s;
        cin>>s;
        sort(s.begin(),s.end());
        //cout<<s<<endl;
        int ji=0,ou=0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]==s[i+1])
            {
                ou++;
                i++;
            }
            else ji++;
        }
        //cout<<ji<<" "<<ou<<endl;
        int sum=2*(ou/k);//基础个数(用偶数进行k分平分)
        ji+=2*(ou%k);//偶数分剩下的继续续给奇数
        if(ji>=k) sum++;//奇数个的时候每个人都还能再分,继续分下去,要回文的话,最短的那一个就只能中间插入一个
        cout<<sum<<endl;
    }
    return 0;
}
posted @ 2022-08-17 14:47  高尔赛凡尔娟  阅读(36)  评论(0编辑  收藏  举报