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

Example

Input
aabc
Output
abba
Input
aabcd
Output
abcba

题目大意:输入一串字符串,改变其中的字母或调整顺序,最后使得输出的字符串为字典序最小的回文,且要求变换次数最小(调准顺序不算)

思路:先使用一个数组,用来记录每个字符出现的次数,再用一个数组来记录个数为奇数的字母,然后从头尾两端向中间遍历,大的减一小的加一。最后再看为奇数字母的个数,若为偶数则按字母从小到大排序即可,若为奇数,则把最后为奇数个的字母放在中间,其余的按从小到大排序。

#include<stdio.h>
#include<string.h>
char str[200000];
int main()
{
int n = 0;
int re[26] = { 0 }, ch[26] = {0}; int len;
scanf("%s",str);
len = strlen(str);
for (int i = 0; i < len; i++)
{
re[str[i]-'a']++;
}
for (int i = 0; i < 26; i++)
{
if (re[i] % 2 != 0)
{
ch[n++] = i;
}
}
for (int i = 0, j = n-1; i < j; i++, j--)
{
re[ch[i]]++;
re[ch[j]]--;
}
if (n % 2 != 0)
{
str[len / 2] = ch[n / 2] + 'a';
}
for (int i = 0, j = len-1,h=0; i <= j; i++, j--)
{
for (; h < 26; h++)
{
if (re[h] >= 2)
{
re[h] -= 2;
str[i] = h+'a';
str[j] = h+'a';
break;
}
}
}
printf("%s", str); return 0;
}

CodeForces - 600C Make Palindrome 贪心的更多相关文章

  1. codeforces 600C Make Palindrome

    要保证变化次数最少就是出现次数为奇数的相互转化,而且对应字母只改变一次.保证字典序小就是字典序大的字母变成字典序小的字母. 长度n为偶数时候,次数为奇数的有偶数个,按照上面说的搞就好了. n为奇数时, ...

  2. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  3. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  4. Educational Codeforces Round 2 C. Make Palindrome 贪心

    C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  5. Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串

    题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...

  6. Make Palindrome CodeForces - 600C(思维)

    A string is called palindrome if it reads the same from left to right and from right to left. For ex ...

  7. Codeforces Round 486C - Palindrome Transformation 贪心

    C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...

  8. 【Codeforces 600C】Make Palindrome

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 计算出来每个字母出现的次数. 把字典序大的奇数出现次数的字母换成字典序小的奇数出现次数的字母贪心即可. 注意只有一个字母的情况 然后贪心地把字 ...

  9. CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)

    题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...

随机推荐

  1. 小记 百度地图 soso地图 经纬度偏移

    项目里遇到了这么个问题,数据库原有数据是微信上用的,所以是soso地图坐标, 但是现在要做百度地图,坐标偏移严重,网上找了也没说偏移多少,自己手动测试10多分钟,得到个大概值,反正差不多就行了. so ...

  2. [转载]C语言程序的内存分配方式

    "声明一个数组时,编译器将根据声明所指定的元素数量为数量为数组保留内存空间."其实就是编译器在编译的过程中,会加入几条汇编指令在程序里处理内存分配,并不是说编译时就分配了内存,不要 ...

  3. Linux IO模型

    1. Linux IO 模型矩阵 2. 同步阻塞IO 3. 同步非阻塞IO 4. 异步阻塞IO 5. 异步非阻塞IO

  4. Linux Shell管道调用用户定义函数(使shell支持map函数式特性)

    Linux中有一个管道的概念,常用来流式的处理文本内容,比如一个文件对其中的每一行应用好几个操作,出于两个方面的考虑可能需要在管道中使用用户定义函数: 1. 刚需: 内置的sed/awk之类的可能没法 ...

  5. Shell基础-Bash命令类型

    Bash命令大体可以分为两类: 第一类是可执行文件,例如ls等 第二类是Bash内建命令,常见echo,cd等 bash命令优先级表:1别名  由alias指定 2关键字 3函数  由function ...

  6. 说说C语言运算符的“优先级”与“结合性”

    论坛和博客上常常看到关于C语言中运算符的迷惑,甚至是错误的解读.这样的迷惑或解读大都发生在表达式中存在着较为复杂的副作用时.但从本质上看,仍然是概念理解上的偏差.本文试图通过对三个典型表达式的分析,集 ...

  7. [转]C++ 取代switch的三种方法

    1.常规switch enum EnumType { enumOne, enumTwo, enumThree }; void showMessage(int type) { switch(type) ...

  8. 2016.6.21——Add Binary

    Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...

  9. imperva 获取gti文档

    SSH到设备(MX或GW) 以root用户身份登录MX和GW 运行“impctl support get-tech-info --last-server-archives=5 --caes-numbe ...

  10. 一步一步搭建11gR2 rac+dg之DG 机器配置(七)【转】

    DG 机器配置 转自: 一步一步搭建11gR2 rac+dg之DG 机器配置(七)-lhrbest-ITPUB博客http://blog.itpub.net/26736162/viewspace-12 ...