要保证变化次数最少就是出现次数为奇数的相互转化,而且对应字母只改变一次。保证字典序小就是字典序大的字母变成字典序小的字母。

长度n为偶数时候,次数为奇数的有偶数个,按照上面说的搞就好了。

n为奇数时,要考虑最后中间那个字母。交换法可以证明,其实是贪心最后没有转化掉的字母。

#include<bits/stdc++.h>
using namespace std; typedef long long ll; const int LEN = 2e5+;
char s[LEN]; int cnt[]; //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
gets(s);
int i = , j;
for(; s[i]; i++){
cnt[s[i]-'a']++;
}
int n = i;
i = ; j = ;
while(i < j){
if((cnt[i]&) && (cnt[j]&)) cnt[i++]++,cnt[j--]--;
else {
if(cnt[i]&^) i++;
if(cnt[j]&^) j--;
}
}
int md = -;
if(n&) md = i;
j = ;
for(i = ; i < ; i++){
cnt[i] >>= ;
while(cnt[i]--){
s[j++] = i+'a';
}
}
if(n&){
s[j++] = md+'a'; s[j] = ;
printf("%s",s);
for(i = j-; i >= ; i--) putchar(s[i]);
}
else {
s[j] = ;
printf("%s",s);
for(i = j-; i >= ; i--) putchar(s[i]);
}
puts("");
return ;
}

codeforces 600C Make Palindrome的更多相关文章

  1. CodeForces - 600C Make Palindrome 贪心

    A string is called palindrome if it reads the same from left to right and from right to left. For ex ...

  2. Make Palindrome CodeForces - 600C(思维)

    A string is called palindrome if it reads the same from left to right and from right to left. For ex ...

  3. 【Codeforces 600C】Make Palindrome

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 计算出来每个字母出现的次数. 把字典序大的奇数出现次数的字母换成字典序小的奇数出现次数的字母贪心即可. 注意只有一个字母的情况 然后贪心地把字 ...

  4. Codeforces Round 486C - Palindrome Transformation 贪心

    C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...

  5. Codeforces Gym100952 C. Palindrome Again !!-回文字符串 (2015 HIAST Collegiate Programming Contest)

      C. Palindrome Again !!   time limit per test 1 second memory limit per test 64 megabytes input sta ...

  6. codeforce 600C - Make Palindrome

    练习string 最小变换次数下,且字典序最小输出回文串. #include <cstdio> #include <cstring> #include <cmath> ...

  7. CodeForces 600C【构造】

    题意: 在原字符串中修改数量最少,然后保证最小字典序. #include <bits/stdc++.h> using namespace std; typedef long long LL ...

  8. 【Codeforces 486C】Palindrome Transformation

    [链接] 我是链接,点我呀:) [题意] 光标一开始在p的位置 你可以用上下左右四个键位移动光标(左右)或者更改光标所在的字符(上下增加或减少ascill码) 问你最少要操作多少次才能使得字符串变成回 ...

  9. Codeforces 1304B. Longest Palindrome

    根据数据范围,暴力可以解决,对每一个串,找与其互为回文的串,或者判断自身是否为回文串,然后两两将互为回文的串排列在头尾,中间放且只能最多放一个自身为回文串的串,因为题目说每个串都是不同的 #inclu ...

随机推荐

  1. Ubuntu使用技巧

    命令 获取系统安装包的编译源码及脚本 apt-get source package 查询端口被占用的进程 lsof -i:端口号 配置 配置阿里源 # mv /etc/apt/source.list ...

  2. HBase HA + Hadoop HA 搭建

    HBase 使用的是 1.2.9 的版本.  Hadoop HA 的搭建见我的另外一篇:Hadoop 2.7.3 HA 搭建及遇到的一些问题 以下目录均为 HBase 解压后的目录. 1. 修改 co ...

  3. ASP.NET控件之RequiredFieldValidator控件

    作用:对textbox或者其他输入框进行非空验证: 属性:ControlToValidate (选择要指向的控件) ErrorMessage(错误,输入要显示的错误信息) 应用方法: 原型: Demo ...

  4. 解读人:刘佳维,Spectral Clustering Improves Label-Free Quantification of Low-Abundant Proteins(谱图聚类改善了低丰度蛋白的无标记定量)

    发表时间:(2019年4月) IF:3.95 单位: 维也纳医科大学: 欧洲生物信息研究所(EMBL-EBI): 分子病理学研究所: 奥地利科学院分子生物技术研究所: Gregor Mendel分子植 ...

  5. Java Script 第二章.

    对象: JavaScript中的所有事物都是对象:字符串,数组,数值,函数..... JavaScript中提供多个内建对象,比如说 String,  Date,  Array等等.对象只是带有属性和 ...

  6. iOS sqlite

    iOS sqlite数据库操作.步骤是: 先加入sqlite开发库libsqlite3.dylib, 新建或打开数据库, 创建数据表, 插入数据, 查询数据并打印 1.新建项目sqliteDemo,添 ...

  7. python之Selenium库的使用

    一  什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并 ...

  8. django 请求生命周期

    详细例子:

  9. GUI的最终选择 Tkinter(二):Label和Button组件

    Label组件 Lable组件是用于界面上输出描述的标签,例如提示用户“您下载的电影含有未成年人限制内容,请满18岁以后点击观看!”,先来上结果图: 在来看下它的代码: from tkinter im ...

  10. git commit之后撤销

    先git log 查看日志,找到需要回退的那次commit的哈希值 然后git reset --soft commit_id ok