Educational Codeforces Round 2 600C Make Palindrome(脑洞)

来源:互联网 发布:大数据热门新闻 编辑:程序博客网 时间:2024/05/29 15:59

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



题目链接: 点击打开链接

给出一个字符串, 可以更改任意位置字符或重新排序字符串使得字符串为回文, 要求变换最少次数, 输出变换后的字符串.

计数字符的个数, 遍历每个字符, 若当前字符为奇数, 则从后向前寻找个数为奇数的字符, 若找到的字符为本身, 则将当前字符放到字符串

中间, 否则替换为当前字符.

AC 代码:

#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"#include "queue"#include "stack"#include "cmath"#include "utility"#include "map"#include "set"#include "vector"#include "list"#include "string"#include "cstdlib"using namespace std;typedef long long ll;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;const int MAXN = 30;string s;int num[MAXN];int main(int argc, char const *argv[]){cin >> s;int len = s.size(), x = 25, pos = 0;for(int i = 0; i < len; ++i)num[s[i] - 'a']++;for(int i = 0; i < 26; ++i) {if(num[i] & 1) {while(num[x] % 2 == 0 && i < x) x--;if(i == x) {num[i]--;s[len / 2] = i + 'a';}else {num[x]--;num[i]++;}}for(int j = 0; j < num[i] / 2; ++j) {s[pos] = i + 'a';s[len - pos - 1] = i + 'a';pos++;}}cout << s << endl;return 0;}


1 0
原创粉丝点击