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 水题,但是坑了很多人.需要注意以下就是正方 ...
随机推荐
- HTTP请求中浏览器缓存
本文导读:浏览器缓存机制,其实主要就是HTTP协议定义的缓存机制.客户端缓存是否需要是可以在服务端代码上控制的.那就是响应头.响应头告诉缓存器不要保留缓存,缓存器就不会缓存相应内容:如果请求信息是需要 ...
- Oracle 课程八之性能优化之Oracle SQL Trace
一. SQL_TRACE 当SQL语句出现性能问题时,我们可以用SQL_TRACE来跟踪SQL的执行情况,通过跟踪,我们可以了解一条SQL或者PL/SQL包的运行情况,SQL_TRACE命令会将SQL ...
- HDU 5122 K.Bro Sorting
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...
- android Vibrator 使用
private Vibrator vibrator; 取得震动服务的句柄 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); 或者 vi ...
- hdu1792 水题
最近转到vim上来了,用vim编写代码,用gcc编译,用gdb调试.这是用vim做的第一道题,纪念下.题目很水,就不说了. /* * Author : ben */ #include <cstd ...
- Loadrunner模拟Json请求
一.loadrunner脚本创建 1.Insert - New step -选择Custom Request - web_custom_request 2.填入相应参数 3.生成脚本,并修改如下(参数 ...
- 学习笔记——XSLT转换器的使用(Xalan和Saxon) .(转)
转自:http://blog.csdn.net/crystalbruce/article/details/7401602 XSLT分为两类: 1:客户端转换:需要浏览器的支持. 2:服务器转换:需要使 ...
- ASM中的别名
在ASM中的别名,是为了方便管理. 在ASM中创建别名,一种是在RDBMS中创建,另外一种是在ASM中创建,区别就是在ASM中创建的别名,在RDBMS中是不可见的. 在RDBMS中创建别名: SQL& ...
- Backbone.js developer 武汉 年薪8w-10w
1. 精通Backbone.js 2. 熟练Ajax.NoSQL.RESTful APIs 3. 了解Pusher.com和 Parse.com 4. 具有良好的沟通能力,学习能力,敬 ...
- [算法] 冒泡排序 Bubble Sort
冒泡排序(Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...