Educational Codeforces Round 2-C. Make Palindrome

来源:互联网 发布:ubuntu文本输入 编辑:程序博客网 时间:2024/06/05 01:04

原题链接

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


对于输入的字符串,记录每种字符的个数,从小到大遍历每种字符的个数num[], 找到字符个数为奇数的num[i], 从大到小遍历每种字符,找到字符个数为奇数的num[j].num[i]++, num[j]--;以此类推

#include <bits/stdc++.h>#define maxn 200005#define MOD 1000000007using namespace std;typedef long long ll;char str[maxn];int num[maxn];int main(){//freopen("in.txt", "r", stdin);scanf("%s", str);for(int i = 0; str[i]; i++){num[str[i]-'a']++;}int len = strlen(str);    int l = 0, r = 25;    while(l < r){    for(; l < r; l++){    if(num[l] % 2 == 1){    break;    }    }    for(; r > l; r--){    if(num[r] % 2 == 1){    break;    }    }    if(l != r){    num[l]++;    num[r]--;    }    }    int cnt = 0, k = -1;    for(int i = 0; i < 26; i++){    if(num[i]&1){    k = i;    num[i]--;    }    while(num[i]){    str[cnt] = i + 'a';    str[len-cnt-1] = i +'a';    cnt++;    num[i] -= 2;    }    }    if(k != -1)    str[cnt] =  k + 'a';    puts(str);    return 0;}


0 0