【Codeforces 600C. Make Palindrome】& 构造
2017-08-23 18:05
302 查看
C. Make Palindrome
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
题意 : 给出一串字符串,每次可以选取一个字符把它变成其他字符,可以交换字符的位置,构成字典序最小的回文串,且改变的字符次数最少
思路 : 字符个数要么为奇数,要么为偶数,偶数的不用变,奇数的都要变,且变一半就好,又因为字典序最小,自然把大的一半,变成小的一半
AC代码:
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
题意 : 给出一串字符串,每次可以选取一个字符把它变成其他字符,可以交换字符的位置,构成字典序最小的回文串,且改变的字符次数最少
思路 : 字符个数要么为奇数,要么为偶数,偶数的不用变,奇数的都要变,且变一半就好,又因为字典序最小,自然把大的一半,变成小的一半
AC代码:
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; const int MAX = 2e5 + 10; typedef long long LL; char s[MAX]; int n[26]; int main() { scanf("%s",s); int nl = strlen(s); for(int i = 0; i < nl; i++) n[s[i] - 'a']++; int l = 0,r = 25; while(1){ while(n[r] % 2 == 0) r--; while(n[l] % 2 == 0) l++; if(l >= r) break; n[r]--,n[l]++; } l = 0; for(int i = 0,j = nl - 1; i < j; i++,j--){ while(n[l] < 2) l++; n[l] -= 2; s[i] = s[j] = l + 'a'; } if(nl & 1){ l = 0; while(!n[l]) l++; s[nl / 2] = l + 'a'; } printf("%s\n",s); return 0; }
相关文章推荐
- 【思维 && 构造】CodeForces - 148C Terse princess
- 【Codeforces 840 A. Leha and Function】& 构造
- Codeforces Round #209 (Div. 2) B && Codeforces 359 B(构造)
- Codeforces 544B - Sea and Islands(构造)
- Spring(3.2.3) - Beans(2): 属性注入 & 构造注入
- Codeforces 673D Bear and Two Paths (贪心构造)
- 【Codeforces 715C&716E】Digit Tree【树分治】
- codeforces 347 c Alice and Bob(博弈 && 数学)
- 千万不要在构造方法里写 Director::getInstance()->getScheduler();
- CodeForces 534A-A - Exam-构造水题
- CodeForces - 916C(构造)
- 讨论构造拷贝构造的N中调用情况&&总结C++对传参合传返回值时构造的优化处理。
- 【打CF,学算法——三星级】CodeForces 550D Regular Bridge (构造)
- 二段构造->REF引用计数管理->PoolManager自动释放池
- codeforces 891 B. Gluttony(构造)
- 【SRM-06 D】五色战队&&【codeforces 788E】 New task
- 出现sstream:724: 错误:C2248:的原因是流不允许拷贝构造,在使用流的方法里修改为&(引用)即可
- CodeForces 468A 构造
- codeforces 560 C Gerald's Hexagon
- Codeforces 584 A. Olesya and Rodion 数论+找规律(构造)