题目链接:http://codeforces.com/contest/600/problem/C

C. Make Palindrome
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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.

Examples
input
aabc
output
abba
input
aabcd
output
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 —— 贪心 + 回文串的更多相关文章

  1. Educational Codeforces Round 2 C. Make Palindrome 贪心

    C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  2. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  4. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. uva 10716 Evil Straw Warts Live(贪心回文串)

    这道题目我用了一上午才做出来,还是看的别人的思路,尽管没有看代码做的有点慢.代码能力还是得加强啊.思维 得缜密.不能想当然,要有根据,写上的代码要有精确度.省的以后还得慢慢调试 思路:贪心.每次都查看 ...

  7. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

  8. Educational Codeforces Round 12 C. Simple Strings 贪心

    C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...

  9. Educational Codeforces Round 25 D - Suitable Replacement(贪心)

    题目大意:给你字符串s,和t,字符串s中的'?'可以用字符串t中的字符代替,要求使得最后得到的字符串s(可以将s中的字符位置两两交换,任意位置任意次数)中含有的子串t最多. 解题思路: 因为知道s中的 ...

随机推荐

  1. Spring MVC中@ControllerAdvice注解实现全局异常拦截

    在网上很多都把Advice翻译成增强器,其实从翻译工具上看到,这个单词翻译是忠告,通知的意思. 首先这个注解实在Spring Web包下,而Spring MVC离不开Spring Web的依赖,所以经 ...

  2. 使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务。

    使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务.

  3. How to create a freehand tool

    http://forums.esri.com/Thread.asp?c=159&f=1707&t=283694&mc=1 http://blog.sina.com.cn/s/b ...

  4. caffe版faster-RCNN环境搭建

    faster-rcnn提出论文: <Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks& ...

  5. 【工作笔记】Git与Github经常使用使用方法

    Git安装 http://www.liaoxuefeng.com/ Git配置用户信息: git config –global user.name "SCOTT" git conf ...

  6. Markdown基础以及个人经验

    前言 DFRobot论坛今日支持Markdown发帖了: [md] your content here [/md] 非常棒,再也不怕辛辛苦苦排个版,一夜回到解放前.这里介绍一下Markdown写博客发 ...

  7. react 自定义 百度地图(BMap)组件

    1.html 页面引入 相关js public/index.html <!DOCTYPE html> <html lang="en"> <head&g ...

  8. Failure [INSTALL_FAILED_ALREADY_EXISTS]

    1.发生原因 做unity开发的同事说apk无法安装,要我帮忙看下,然后我通过命令adb install安装apk,出现此提示 2.解决过程 首先想到的仍然是安装包已存在的问题,结果使用清理类软件清理 ...

  9. VueJS参数绑定:v-bind:href,v-on:event

    参数绑定HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  10. wdcp新开站点或绑定域名打不开或无法访问的问题

    一 用IP可以打开,但用域名打开网站显示到默认页面1  站点列表里是否有相应的网站信息 2  检查有没站点配置文件后台 >系统管理 >文件管理器 >虚拟主机站点文件(nginx,ap ...