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中的 ...
随机推荐
- Careercup | Chapter 5
5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to inse ...
- bzoj 1835/luogu P2605 : [ZJOI2010]base 基站选址
题目描述 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄不超过Si的范 ...
- Java NIO中的Buffer类
Buffer 缓冲,用于批量读写数据 Buffer是一个抽象类,基本数据类型都有实现类:XxxBuffer,比如ByteBuffer.CharBuffer.IntBuffer.DoubleBu ...
- BZOJ 2131 [scoi2010] 传送带
@(BZOJ)[三分法] Description 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段. 两条传送带分别为线段AB和线段CD. lxhgww在AB上的移动速度为P,在CD上的移 ...
- Windows系统Python包的安装
本文针对于Python初学者,Python老鸟请绕道. 最近同时尝试在Windows10.MacOS.Ubuntu三个系统上安装Python相关的包,整体比较,Ubuntu最简单,Windows10最 ...
- nodeJS一些事儿
node-webkit:开发桌面+WEB混合型应用的神器[大漠穷秋] 展望未来 其实这条路老早就有人在走 网上有很多人在争论,未来究竟是原生的应用会胜出,还是WEB APP会胜出,实际上这两者并不是你 ...
- alibaba fastjson常见问题FAQ
English | 中文 1. 怎么获得fastjson? 你可以通过如下地方下载fastjson: maven中央仓库: http://central.maven.org/maven2/com/al ...
- Allegro16.6和17.0和17.2中将板框导出DXF文件
Allegro16.6和17.0和17.2中将板框导出DXF文件 转载 https://blog.csdn.net/pieces_thinking/article/details/69817600 标 ...
- ElasticSearch 分页检索
在ElasticSearch的多索引和多类别里说到我们在集群中有14个文档匹配我们的(空)搜索语句.单数仅仅有10个文档在hits数组中.我们怎样看到其它文档? 和SQL使用LIMITkeyword返 ...
- 几个经典的TCP通信函数
前言 在TCP通信中要使用到几个非常经典的函数( 点这里参考一个关于它们作用的形象比方 ),本文将对这几个函数进行一个简短的使用说明. socket函数 函数作用:创建一个网际字节流套接字 包含头文件 ...