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

长度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. [CentOS7] 常用工具 之 防暴力破解工具 Fail2ban

    防止暴力破解密码: Fail2ban ==> 用于自动ban掉ip 先用yum search fail2ban看看是否yum源含有fail2ban这个package,若没有的话请yum inst ...

  2. [笔记]解决git本地仓库不能上传到gitee远程仓库的问题

    关键词:git.gitee.码云.上传远程仓库失败 1.gitee有一个远程仓库名字是CommandModel,里面只有两个README文件 2.假如我目录  D:\eclipse\workspace ...

  3. 2017-10-2 清北刷题冲刺班a.m

    一道图论神题 (god) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,只 ...

  4. java8List集合根据对象的属性去重

    import static java.util.Comparator.comparingLong; import static java.util.stream.Collectors.collecti ...

  5. Gson本地和服务器环境不同遇到的Date转换问题 Failed to parse date []: Invalid time zone indicator

    GoogleGson在处理Date格式时有个小陷阱,在不同环境中部署时可能会遇到问题. Gson默认处理Date对象的序列化/反序列化是通过一个SimpleDateFormat对象来实现的,通过下面的 ...

  6. 用户与授权:MySQL系列之六

    一.用户管理 1.用户账号 用户的账号由用户名和HOST俩部分组成('USERNAME'@'HOST') HOST的表示: 主机名 具体IP地址 网段/掩码 可以使用通配符表示,%和_:192.168 ...

  7. Kipmi0 占用CPU 100%

    查看当前正在运行的进程发现kipmi0进程占用率达到100% kipmi -% of the CPUs, %/% comes when the machine is idle? A second is ...

  8. spring boot war

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  9. STP-15-PortFast端口

    PortFast是大家熟知的传统STP和PVST+改进特性,它也是RSTP和MST中标准化的增强特性.实质上,它定义了个一个边界端口.边界端口在启用之后立刻进入转发状态,不产生拓扑变化事件,不会因为处 ...

  10. js对输入文字个数的限制...

    发表留言或者微博的时候,对输入文字个数的限制,下面分两种情况,每种分别介绍两种实现方式: 第一种:一个汉字算一位,两个字母或符号算一位. 通过ASCII编码来判断 textarea.onkeyup = ...