Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
题目链接:http://codeforces.com/contest/600/problem/C
2 seconds
256 megabytes
standard input
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.
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-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 2e5+; int sum[], len;
char s[maxn]; void init()
{
scanf("%s",s+);
len = strlen(s+);
for(int i = ; i<=len; i++)
sum[s[i]-'a']++;
} void solve()
{
int i = , j = ;
while(i<j)
{
while(i<j && !(sum[i]&)) i++;
while(i<j && !(sum[j]&)) j--;
sum[i]++;
sum[j]--;
} for(i = ; i<=; i++)
for(j = ; j<=sum[i]/; j++)
printf("%c",i+'a'); for(i = ; i<=; i++)
if(sum[i]%)
printf("%c",i+'a'); for(i = ; i>=; i--)
for(j = ; j<=sum[i]/; j++)
printf("%c",i+'a');
cout<<endl;
} int main()
{
init();
solve();
}
Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串的更多相关文章
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- uva 10716 Evil Straw Warts Live(贪心回文串)
这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢.代码能力还是得加强啊.思维 得缜密.不能想当然,要有根据,写上的代码要有精确度.省的以后还得慢慢调试 思路:贪心.每次都查看 ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Educational Codeforces Round 12 C. Simple Strings 贪心
C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...
- Educational Codeforces Round 25 D - Suitable Replacement(贪心)
题目大意:给你字符串s,和t,字符串s中的'?'可以用字符串t中的字符代替,要求使得最后得到的字符串s(可以将s中的字符位置两两交换,任意位置任意次数)中含有的子串t最多. 解题思路: 因为知道s中的 ...
随机推荐
- 洛谷—— P3865 【模板】ST表
https://www.luogu.org/problemnew/show/P3865 题目背景 这是一道ST表经典题——静态区间最大值 请注意最大数据时限只有0.8s,数据强度不低,请务必保证你的每 ...
- mysql中TIMESTAMPDIFF简单记录
1. Syntax TIMESTAMPDIFF(unit,begin,end); 根据单位返回时间差,对于传入的begin和end不需要相同的数据结构,可以存在一个为Date一个DateTime 2 ...
- 东方14模拟赛之noip2015/day1/3/神奇的幻方
总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 128000kB 描述 幻方是一种很神奇的N*N 矩阵:它由数字 1,2,3, … …,N*N 构成,且每行.每列及 ...
- [HEOI2015]定价
题目描述 在市场上有很多商品的定价类似于 999 元.4999 元.8999 元这样.它们和 1000 元.5000 元和 9000 元并没有什么本质区别,但是在心理学上会让人感觉便宜很多,因此也是商 ...
- Java中HashMap的初始容量设置
根据阿里巴巴Java开发手册上建议HashMap初始化时设置已知的大小,如果不超过16个,那么设置成默认大小16: 集合初始化时, 指定集合初始值大小. 说明: HashMap使用HashMap(in ...
- fastscripT实现权限控制
fastscripT权限控制 此处以FASTSCRIPT实现功能权限为例,用脚本实现数据权限也是很方便的. unit Unit1; interface uses Winapi.Windows, Win ...
- 邁向IT專家成功之路的三十則鐵律 鐵律四:IT人快速成長之道-複製
相信您一定看到過現今有許多各行各業的成功人士,他們最初都是從複製別人的成功經驗開始的,就算是一位知名的歌手,有許多都是在未成名以前,先行模仿知名歌手的唱腔.舞蹈.服裝等等開始的,然後在慢慢經過自我努力 ...
- 为什么HierachyViewer无法连接真机调试
关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个人理解:Hi ...
- git 怎样删除远程仓库的最近一次错误提交?
假设你有3个commit如下: commit 3 commit 2 commit 1 其中最后一次提交commit 3是错误的,那么可以执行: git reset --hard HEAD~1 你会发现 ...
- Solidworks输出Autocad的DWG格式乱码怎么办
Solidworks输出DWG会有很多问题,如果没必要就别这么做,比如你只是想要打印图纸,Solidworks也可以直接打印,而且很方便,不需要转成DWG再打印,如果对方确实需要DWG格式的图纸,你只 ...