CodeForces 600C Make Palindrome

86 篇文章 0 订阅

题意:

改变最少的字母,得到一个典序最小的回文串。可以重排,重排不算操作次数。

思路

统计个数,如果是偶数,两侧放,奇数的话找一个后面的奇数抵消,如果找不到放在中间

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
using namespace std;
const int maxn=200005;
char s[maxn];
int a[maxn];

int main()
{
    scanf("%s",s);
    for(int i=0;s[i];i++)
        a[ s[i]-'a']++;
    int last=strlen(s)-1,fir=0;
    for(int i=0;i<26;i++)
    {
        while(a[i]!=0)
        {
        if(a[i]%2==0)
        {
            s[fir]=s[last]=i+'a';
            a[i]-=2;
            fir++,last--;
        }
        else
        {
            int flag=0;
            for(int j=25;j>i;j--)
            {
                if(a[j]&1)
                {
                    flag=j;
                    break;
                }
            }
            if(flag)
            {
                a[i]--;
                a[flag]--;
                s[last]=s[fir]=i+'a';
                last--,fir++;
            }
            else
            {
                a[i]--;
                s[strlen(s)/2]=i+'a';
            }
        }
        }
    }
    printf("%s\n",s);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值