Codeforces 600C Make Palindrome 【贪心 找字典序最小回文串】

C. Make Palindrome
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings "abb" and "ij" are not.

You are given string s consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in s. Then you can permute the order of letters as you want. Permutation doesn't count as changes.

You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically.

Input

The only line contains string s (1 ≤ |s| ≤ 2·105) consisting of only lowercase Latin letters.

Output

Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes.

Sample test(s)
input
aabc
output
abba
input
aabcd
output
abcba



题意:给定一个只包含小写字母的字符串,你可以修改任意位置的字符(变换为a-z中任一个),然后重新排列字符串。现在要求用最少次数的修改得到一个回文串,若有多种方案,输出字典序最小的方案。


思路:找出并记录所有字母的出现次数num[],若num[i] & 1说明该字母不能对称放置,这时有两种方案——1,需要把其它字母改为当前字母;2,把该字母改为其它字母。为了得到最小字典序,我们贪心的选择将字典序大的字母改为字典序小的字母,按照这样的思维模拟即可。


注意:串长度len & 1时的处理。


AC代码:


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (200000+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
char str[MAXN]; int num[30];
bool mark[30];
int main()
{
    Rs(str); CLR(num, 0);
    int len = strlen(str);
    for(int i = 0; i < len; i++)
        num[str[i]-'a']++;
    CLR(mark, false); int cnt = 0;
    for(int i = 0; i < 26; i++)
        if(num[i] & 1)
            mark[i] = true, cnt++;
    char mid; int use = 0;
    if(len & 1)
    {
        cnt--;
        for(int i = 0; i <= 25; i++)
        {
            if(mark[i])
                use++;
            if(use > cnt / 2)
            {
                mid = 'a' + i;
                mark[i] = false;
                break;
            }
        }
    }
    bool flag = false; use = 0;
    for(int i = 0; i <= 25; i++)
    {
        if(mark[i])
        {
            if(flag)
                num[i]--;
            else
                num[i]++;
            use++;
        }
        if(use == cnt / 2)
            flag = true;
    }
    int F = 0, B = len-1;
    for(int i = 0; i < 26; i++)
    {
        for(int j = F; j < F + num[i] / 2; j++)
            str[j] = 'a' + i;
        F += num[i] / 2;
        for(int j = B; j > B - num[i] / 2; j--)
            str[j] = 'a' + i;
        B -= num[i] / 2;
    }
    if(len & 1)
        str[len / 2] = mid;
    Ps(str);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值