codeforces_600C. Make Palindrome(贪心)

7 篇文章 0 订阅
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.

Examples
input
aabc
output
abba
input
aabcd
output
abcba
 
   
把奇数个的字母保存在一个数组中,然后用两个指针分别从数组的两端开始往中间扫,后面的指针指向的字母个数减1,前面的相应加1。注意奇数的特殊处理。
 
   
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 200010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

char a[maxn];
int  v[200];
struct node
{
    char alp;
    int pos;
} v2[200];
int main()
{
    //FOP;
    Ss(a);
    int len=strlen(a);
    sort(a,a+len);
    int i,j,fi,top=0;
    char alp1;
    mem(v,0);
    mem(&v2,0);
    for(i=0; i<len; i++)
    {
        v[a[i]]++;
    }
    for(i='a'; i<='z'; i++)
    {
        if(v[i]%2)
        {
            v2[top].alp=i;
            v2[top].pos=v[i];
            top++;
        }
    }
    for(i=top-1,j=0;i>=top/2;i--,j++)
    {
        v[v2[i].alp]--;
        v[v2[j].alp]++;
    }
    alp1=v2[top/2].alp;
    for(i='a'; i<='z'; i++)
    {
        for(j=0; j<v[i]/2; j++)
        {
            printf("%c",i);
        }
    }
    if(len%2)
    {
        printf("%c",alp1);
    }
    for(i='z'; i>='a'; i--)
    {
        for(j=0; j<v[i]/2; j++)
        {
            printf("%c",i);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值