Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/600/problem/C
Description
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 Input
aabc
Sample Output
abba
HINT
题意
给你一个字符串,让你重新排列,并且你也可以修改一些字符
问你在保证修改最少的情况下,字典序最小的回文串是什么样子的?
题解:
串长为偶数则所有字母出现次数均为偶数,把所有出现次数为奇数的都换一个变成a即可,
串长为奇数,那么至多有一种字母出现次数为奇数,选取字典序最小的那种,其余出现次数为奇数的都换一个变成a即可
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std; string s;
int p[];
int dp[];
int main()
{
cin>>s;
for(int i=;i<s.size();i++)
p[s[i]-'a']++;
int d = s.size()-;
int flag = -;
for(int i=;i<;i++)
{
while(p[i]>=)
{
flag++;
dp[flag]=i;
p[i]-=;
}
}
/*
for(int i = odd.size() / 2 ; i < odd.size() ; ++ i ){
char r = odd[i].first;
char l = odd[i-odd.size()/2].first;
cnt[r]--;
cnt[l]++;
}
*/
for(int i=;i<;i++)
{
if(p[i])
{
flag++;
if(*flag==d)
{
dp[flag]=i;
break;
}
if(*flag>d)break;
dp[flag]=i;
}
}
while(*flag<d)flag=(d+)/;
sort(dp,dp+flag);
for(int i=;i<flag;i++)
dp[s.size()-i-]=dp[i];
for(int i=;i<s.size();i++)
printf("%c",dp[i]+'a');
}
Educational Codeforces Round 2 C. Make Palindrome 贪心的更多相关文章
- Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...
- 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中的 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 32
http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
随机推荐
- memcached 最大连接数及其内存大小的设置
memcached的基本设置: -p 监听的端口-l 连接的IP地址, 默认是本机-d start 启动memcached服务-d restart 重起memcached服务-d stop|shutd ...
- 底部菜单栏(一) TabHost实现
需求:使用TabHost实现底部菜单栏: 效果图: 实现分析: 1.目录结构: 代码实现: 1.activity_main.xml <?xml version="1.0" e ...
- 通过ListActivity使用ListView布局方法
先简单的介绍一下ListActivity ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来.ListA ...
- Hadoop序列化
遗留问题: Hadoop序列化可以复用对象,是在哪里复用的? 介绍Hadoop序列化机制 Hadoop序列化机制详解 Hadoop序列化的核心 Hadoop序列化的比较接口 ObjectWrita ...
- Procdure for wanfo business report
CREATE OR REPLACE PROCEDURE PROC_TZ_EXEC_N_YEARREPORT(ssrq varchar2 ) as -----声明变量 v_raise EXCEPTION ...
- cocos2d-x知识巩固-基础篇(1)
有段时间没有学习cocos2dx了,作为新人,自己觉得还是要稳扎稳打,一点点的去积累,梳理好每一个知识点,这样对自己的成长能够有一个更清晰的认识,以便做更好的提高. 从2013年8月开始接触cocos ...
- 重新起步 iOS 开发
25Dec2013 Stanford iOS 公开课看到第三课 Programing in Objective-C 2.0 看完了第一部分,基本是以半小时一章的速度浏览的
- OpenStack的Resize和冷迁移代码解析及改进
原文:http://www.hengtianyun.com/download-show-id-79.html OpenStack的Resize(升级)功能,我们可以改变虚拟机的CPU核数.内存及磁盘大 ...
- 【转】iOS开发工具系列(按功能分)
http://www.cocoachina.com/newbie/basic/2014/0417/8187.html 这是我们多篇iOS开发工具系列篇中的一篇,此前的文章比如:那些不能错过的Xcode ...
- Gradle – Spring 4 MVC Hello World Example – Annotation
In this tutorial, we will take the previous Gradle + Spring MVC XML example, rewrite it to support @ ...