CodeForces 600C Make Palindrome 贪心

来源:互联网 发布:淘宝客服问题总结 编辑:程序博客网 时间:2024/05/18 02:18

题意:给出字符串s,通过调整顺序或修改字符使字符串回文,输出修改次数最小且紫苜蓿最小的回文字符串。

统计一下字母次数,字母序后的出现次数奇数的改成字母序小的,然后贪心选能使用的最小字符构建回文字符串即可。

#include <cstdio>#include <cstring>int main() {    int i, j, k, len, s[26]={0}, w[26]={0}, wc = 0;    static char str[2000001];    scanf("%s", str + 1);    len = strlen(str + 1);    for (i = 1; i <= len; i++)        s[str[i] - 'a']++;    for (i = 0; i < 26; i++)        if (s[i] & 1) w[++wc] = i;    for (i = 1, j = wc; i < j; i++, j--)        s[w[j]]--, s[w[i]]++;    if (wc & 1)        str[len / 2 + 1] = w[wc / 2 + 1] + 'a';    for (i = 1, j = len, k = 0; i <= j; i++, j--)        for (; k < 26; k++)            if (s[k] > 1) { s[k] -= 2; str[i] = str[j] = k + 'a'; break; }        printf("%s", str + 1);    return 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.

Sample test(s)
input
aabc
output
abba
input
aabcd
output
abcba


0 0
原创粉丝点击