Make Palindrome CodeForces - 600C(思维)

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

就是用最小的改变次数把这个字符串 转换成回文串
可以移动字符的位置 不计入总次数
如果好几个次数相同的 取字典序最小的
统计每个单词的个数 从前 后分别 找到个数为奇数的单词 前面的++ 后边的--
如果只有一个 放到中间输出即可
原字符串长度为偶数的序列 不存在奇数个 个数奇数个的单词
#include <bits/stdc++.h>
using namespace std;
string str;
int num[28];
vector<char> v;
int main()
{
    cin>> str;
    int len = str.size();
    for(int i=0; i<len; i++)
        num[str[i]-'a']++;
    int j = 25;
    int flag;
    for(int i=0; i<26; i++)
    {
        if(num[i] & 1)
        {
            while(!(num[j] & 1) && j >= i) j--;
            if((num[j] & 1) && j > i)
            {
                num[j]--;
                num[i]++;
            }
            if(i == j)
                flag = i;
        }
        if(num[i])
            for(int k=0; k<num[i]/2; k++)
            {
                char tmp = 'a' + i;
                printf("%c", tmp);
                v.push_back(tmp);
            }
    }
    if(len & 1)
        printf("%c", 'a'+flag);
    for(int i=v.size()-1; i>=0; i--)
        cout<< v[i]; 
    cout<< endl;
    
    return 0;
} 

 

posted @ 2018-09-07 11:57  WTSRUVF  阅读(220)  评论(0编辑  收藏  举报