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;
}

最新文章

  1. 配置与使用 Git与Github
  2. 使用CTex完成北京科技大学本科生毕业设计
  3. Uva1515 Pool construction
  4. hdu1019 Least Common Multiple
  5. 【转】iOS bitcode实战 -- without full bitcode
  6. 关于android4.3 Intel X86 Atom System Image的下载
  7. Java基础-四要素之一《封装》
  8. [BZOJ 1053] [HAOI 2007] 反素数ant
  9. Linux怪哉ntfs
  10. HTML5+移动APP(2)
  11. Java核心技术 卷Ⅰ 基础知识(1)
  12. D3D中一些接口的变化和VS配置关联的方法
  13. javascript中DOM集锦(二)
  14. 微信小程序分包跳转主包页面
  15. pHP生成唯一单号
  16. 记录 用tiny6410 j-link eclipse 在线调试裸机代码leds
  17. 如何在eclipse添加SVN菜单
  18. 【laravel5.6】 laravel 执行 php artisan route:cache 报错 Unable to prepare route [/] for serialization. Uses Closure.
  19. java中,什么是方法的重载?需要满足什么条件?两同三不同指的什么?
  20. javascript面向对象之Object.defineProperty(a,b,c)

热门文章

  1. Table of Contents - Python 2.x
  2. pandas 视频讲座 from youtube
  3. JavaScript事件代理入门
  4. tomcat关闭后没有及时释放内存资源
  5. Ubuntu 通过 Live CD 更新grub恢复引导Boot Menu
  6. v4l2API无法执行VIDIOC_DQBUF的问题
  7. torch.Tensor.view (Python method, in torch.Tensor)
  8. MVVM模式View和ViewModel的通信
  9. C基础 旋转数组查找题目
  10. python面向对象(七)属性方法的添加