【Codeforces 600C. Make Palindrome】& 构造
来源:互联网 发布:js怎么获取input的值 编辑:程序博客网 时间:2024/05/16 00:40
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代码:
#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 600C. Make Palindrome】& 构造
- Educational Codeforces Round 2C. Make Palindrome(贪心构造)
- CodeForces 600C Make Palindrome 贪心
- codeforces 600C. Make Palindrome(贪心)
- CodeForces 600C Make Palindrome (模拟题)
- Educational Codeforces Round 2 600C Make Palindrome(脑洞)
- Educational Codeforces Round 2-C. Make Palindrome
- Codeforces 600C Make Palindrome 【贪心 找字典序最小回文串】
- Educational Codeforces Round 2 C.Make Palindrome(贪心)
- codeforces 486C Palindrome Transformation 贪心求构造回文
- codeforces 486C Palindrome Transformation 贪心求构造回文
- CodeForces 600C【构造】
- Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
- CodeForces 486C Palindrome Transformation
- Codeforces 486 C Palindrome Transformation
- codeForces 486C Palindrome Transformation
- CodeForces 600CMake Palindrome
- Make Palindrome
- Day11-37.if-else
- SVD 电影评分问题
- 判断是否为连续的数字拼接而成的字符串
- jquery mobile 跳转问题
- 简单的动态顺序表实现 不足之处请留言指出
- 【Codeforces 600C. Make Palindrome】& 构造
- shiro整合spring
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- 设计模式C++版:第十五式职责链模式
- String类型相加随笔
- c# 操作注册表
- IOS学习之Layout
- ios-UITextView 输入字数限制,一行代码搞定
- springboot异常处理