Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
来源:互联网 发布:mac怎样整理文件 编辑:程序博客网 时间:2024/06/04 17:55
题目链接:http://codeforces.com/contest/600/problem/C
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.
The only line contains string s (1 ≤ |s| ≤ 2·105) consisting of only lowercase Latin letters.
Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes.
aabc
abba
aabcd
abcba
题解:
1.统计每个字母的出现次数。
2.最少操作次数:假设出现了奇数次的字母共有t个,则最少操作次数为t/2(除法为向下取整,下同),使得所有字母均出现偶数次(长度为偶数时),或者一个字母出现奇数次,其他字母出现偶数次(长度为奇数时)。
3.在操作次数为最小的情况下,尽可能降低总的ASCII值。即把ASCII值大且出现奇数次的字母变一个成ASCII值小且出现奇数次的字母,这样两种字母都出现偶数次了。
4.排列:按ASCII值从小到大,每个字母先输出sum[i]/2个,如果长度为奇数,则还需把中间的输出来,然后再按ASCII值从大到小,每个字母输出sum[i]/2个。
代码如下:
#include<bits/stdc++.h>using namespace std;typedef long long LL;const double eps = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 2e5+10;int sum[30], len;char s[maxn];void init(){ scanf("%s",s+1); len = strlen(s+1); for(int i = 1; i<=len; i++) sum[s[i]-'a']++;}void solve(){ int i = 0, j = 25; while(i<j) { while(i<j && !(sum[i]&1)) i++; while(i<j && !(sum[j]&1)) j--; sum[i]++; sum[j]--; } for(i = 0; i<=25; i++) for(j = 1; j<=sum[i]/2; j++) printf("%c",i+'a'); for(i = 0; i<=25; i++) if(sum[i]%2) printf("%c",i+'a'); for(i = 25; i>=0; i--) for(j = 1; j<=sum[i]/2; j++) printf("%c",i+'a'); cout<<endl;}int main(){ init(); solve();}
- Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
- Educational Codeforces Round 2C. Make Palindrome(贪心构造)
- Educational Codeforces Round 2 C.Make Palindrome(贪心)
- Educational Codeforces Round 2-C. Make Palindrome
- Educational Codeforces Round 2 600C Make Palindrome(脑洞)
- 【Educational Codeforces Round 2C】【贪心】最少修改数下得到字典序尽可能小回文串
- Codeforces 600C Make Palindrome 【贪心 找字典序最小回文串】
- Educational Codeforces Round 12(C)贪心
- Codeforces Round #389 (Div. 2) D. Santa Claus and a Palindrome 回文串+贪心+数据结构、字符串
- Educational Codeforces Round 26 A—C
- CodeForces 600C Make Palindrome 贪心
- codeforces 600C. Make Palindrome(贪心)
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
- Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
- Codeforces Round #277 (Div. 2) C. Palindrome Transformations 贪心
- Codeforces Educational Codeforces Round 3 C. Load Balancing(贪心)
- Educational Codeforces Round 6(C)尺取法+贪心
- Educational Codeforces Round 6 C. Pearls in a Row(贪心)
- jd.com ajax post的登录操作
- 交叉编译openssl,不用修改Makefile编译工具
- oracle 11g 查看服务端/客户端编码,及修改db编码
- JS面向对象的程序设计
- Python的zip函数
- Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
- DOS DIR 命令 详解
- C++学习笔记七之使用数组区间的函数
- SpringMVC 定时任务的配置
- 媒体报道 | 创业邦:巴别鸟,一款便捷好用的企业级文件协同网盘
- github vs gitlab 工作量统计
- 【jzoj5219】【GDOI2018模拟7.10】【B】【动态规划】
- 配置采购订单审批报错: Please check release classes (see long text)
- Android 使用数学函数(Math函数)