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 水题,但是坑了很多人.需要注意以下就是正方 ...
随机推荐
- 【CSS】使用CSS改变超链接样式
超链接代码 <ahrefahref="http://www.divCSS5.com/"target="_blank" title="关于divC ...
- HTML5之video元素
一.video元素支持的视频格式 HTML5中的video标签支持3种常用的视频格式: 1.Ogg = 带有Theora 视频编码和Vorbis 音频编码的 Ogg 文件: 2.MPEG4 = 带有H ...
- metadata lock
1 锁等待的例子 session 1: 执行查询但不提交 mysql> begin; mysql> select * from test where c2 = '1'; session 2 ...
- 遍历 集合 Dictionary 的时候修改集合 方法
Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("1" ...
- ASP.NET中常用的字符串分割函数
asp.net字符串分割函数用法 先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我 ...
- springMVC(注解版笔记)
springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...
- 1.1……什么是3G
移动通信技术的发展 第一代移动通信技术(1st - Generation),只能进行语音通话. 第二代移动通信技术(2nd - Generation),可以收发短信.可以上网,但速度只有几十Kbps, ...
- HBase Shell手动移动Region
在生产环境中很有可能有那么几个Region比较大,但是都运行在同一个Regionserver中. 这个时候就需要手动将region移动到负载低的Regionserver中. 步骤: 1.找到要移动的r ...
- 【移动开发】安卓Lab2(01)
本次Lab需要用到Google Map的API,分享学习一下Google Map的知识 需求: 界面: 1. 主界面(map界面): 提供了指定的学校校园地图图片,不能用Google的API生成图片 ...
- HDU-4738 Caocao's Bridges 边联通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:在有重边的无向图中,求权值最小的桥. 注意trick就好了,ans为0时输出1,总要有一个 ...