Educational Codeforces Round 2 C.Make Palindrome(贪心)
2016-01-17 11:59
417 查看
Educational Codeforces Round 2B: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.
Sample test(s)
input
output
input
output
题目大意:给定一个字符串,可以改变一个字母,也可以改变字母的顺序(不算作改变字母),求最小改变次数下的最小回文串。
大致思路:贪心即可,从a开始取字母,每次取2个字母弄在字符串收尾,若当前前字母只剩一个,在后面找一个单数次数的字母改变,若不存在则为最中间的字母
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.
Sample test(s)
input
aabc
output
abba
input
aabcd
output
abcba
题目大意:给定一个字符串,可以改变一个字母,也可以改变字母的顺序(不算作改变字母),求最小改变次数下的最小回文串。
大致思路:贪心即可,从a开始取字母,每次取2个字母弄在字符串收尾,若当前前字母只剩一个,在后面找一个单数次数的字母改变,若不存在则为最中间的字母
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int len,num[257],i,j,sig,cur; char s[200005]; int main() { while(1==scanf("%s",s)) { len=strlen(s); cur=sig=0; j='z'; memset(num,0,sizeof(num)); for(i=0;i<len;++i) ++num[s[i]]; for(i='a';i<='z';++i) if(num[i]&1) ++sig; for(i='a';i<='z';++i) { while(num[i]>1) { s[cur]=s[len-cur-1]=i; num[i]-=2; ++cur; } if(num[i]) { if(sig>1) { while((num[j]&1)!=1) --j; --num[j]; --num[i]; sig-=2; s[cur]=s[len-cur-1]=i; ++cur; } else if(sig==1) { s[len>>1]=i; sig=0; } } } printf("%s\n",s); } return 0; }
相关文章推荐
- 在Linux下如何使用GCC编译程序、简单生成静态库及动态库。
- P1025 数的划分
- Linux下Gcc生成和使用静态库和动态库详解(转)
- constexpr与常量表达式(c++11标准)
- sqlite 交叉编译-转
- 严重: Exception starting filter struts2 java.lang.NullPointerException
- sqlite配置选项
- 关于空调安装时外机支架另收费
- contiki学习笔记---process结构体
- 常用linux命令大全
- Activiti工作流(二)——入门Demo及数据库
- GNS3 icmp之严格路由
- 使用JMX查看一个简单的main方法运行有多少个线程参与
- Java 多线程:AtomicInteger源码分析——基于CAS的乐观锁实现
- CSS 中的继承性,特殊性,层叠性和重要性
- 常用的adb命令
- 百度收录信息抓取
- Textarea换行输出到前端无效果的推荐处理方法
- UI组件之AdapterView及其子类(五)ListView组件和ListActivity
- Cstyle的UEFI导读:第20.0篇 IGD OpRegion interface && IGD OpRegion PROTOCOL