【Codeforces 600C】Make Palindrome
【链接】 我是链接,点我呀:)
【题意】
题意
【题解】
计算出来每个字母出现的次数。
把字典序大的奇数出现次数的字母换成字典序小的奇数出现次数的字母贪心即可。
注意只有一个字母的情况
然后贪心地把字典序小的字母放在前面就好
【代码】
#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define ll long long
using namespace std;
const int N = 2e5;
string s;
char ans[N+10];
int cnt[30];
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin >> s;
rep1(i,0,(int)s.size()-1) cnt[s[i]-'a'+1]++;
for (int i = 1;i <= 26;i++)
if (cnt[i]&1){
for (int j = 26;j >= i+1;j--)
if (cnt[j]&1){
cnt[i]++;
cnt[j]--;
break;
}
}
rep1(i,1,(int)s.size()/2){
for (int j = 1;j <= 26;j++){
if ( (cnt[j]>0 && cnt[j]%2==0) || (cnt[j]>2) ){
ans[i] = (char)(j+'a'-1);
ans[(int)s.size()-i+1] = ans[i];
cnt[j]-=2;
break;
}
}
}
if ((int)s.size()%2==1){
for (int j = 1;j <= 26;j++)
if (cnt[j]%2==1)
ans[(int)s.size()/2+1] = (char)(j+'a'-1);
}
for (int i = 1;i <= (int)s.size();i++){
cout<<ans[i];
}
return 0;
}
【Codeforces 600C】Make Palindrome的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 798A】Mike and palindrome
[题目链接]:http://codeforces.com/contest/798/problem/A [题意] 让你严格改变一个字符,使得改变后的字符串为一个回文串; 让你输出可不可能; [题解] 直 ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【34.88%】【codeforces 569C】Primes or Palindromes?
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【25.64%】【codeforces 570E】Pig and Palindromes
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【44.19%】【codeforces 608D】Zuma
time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
随机推荐
- matlab绘制曲线对比图
>> clear;>> x1=[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8];>> y1=[0,0.55,0.69,0.86,0.93,0. ...
- Tomcat启动Web.xml引用其它XML配置报FileNotFound异常解决方案
如果使用JEECG框架进行Tomcat启动时,如果web.xml引用了其他xml文件,需要在tomcat文件夹里的config文件夹里的context.xml文件里的Context标签里配置xmlBl ...
- js原始数据类型和引用数据类型=>callback数据传输原理
摘要:js的数据类型有种划分方式为 原始数据类型和 引用数据类型. 原始数据类型 存储在栈(stack)中的简单数据段,也就是说,它们的值直接存储在变量访问的位置.栈区包括了 变量的标识符和变量的值. ...
- redis 缓存应用
第1章 部署与安装 wget http://download.redis.io/releases/redis-3.2.10.tar.gz tar xf redis-3.2.10.tar.gz cd r ...
- 【js】再谈移动端的模态框实现
移动端模态框的机制因为与PC的模态框机制一直有所区别,一直是许多新人很容易踩坑的地方,最近笔者作为一条老咸鱼也踩进了一个新坑中,真是平日里代码读得太粗略,故而写上几笔,以儆效尤. 故事的起因是这样的, ...
- [POJ3417]Network/闇の連鎖
Description 传说中的暗之连锁被人们称为 Dark. Dark 是人类内心的黑暗的产物,古今中外的勇者们都试图打倒它.经过研究,你发现 Dark 呈现无向图的结构,图中有 N 个节点和两类边 ...
- [NOI2004]cashier 郁闷的出纳员
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
- 题解报告:hdu 1035 Robot Motion(简单搜索一遍)
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- 378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [ [ 1, 5, 9], [ ...
- Protocol(协议)
Protocol(协议) (一) (1)简介 1.Protocol:就一个用途,用来声明一大堆的方法(不能声明成员变量),不能写实现.看起来类似于一个类的接口, 不同的是协议没有父类,也不能定义实例变 ...