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

长度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. 2017-10-17 NOIP模拟赛

    Reverse #include<iostream> #include<cstdio> #include<cstring> using namespace std; ...

  2. Java基础--正则表达式的规则

    注意:正则表达式只关注格式是否正确,不关注内容是否有效. 一.字符集合, []表示一个字符. 1.[abc] :指a,b,c中的任意一个字符. 2.[^abc]:指除了a,b,c外的任意字符. 3.[ ...

  3. Nacos-spring-samples解析

    小白们在看这个用例的时候得注意,这个东东不知道他是为了让大家能够快速体验还是怎么的, 反正我一开始没整明白,有点想当然的去理解了: 我一直以为这个Nacos-spring-samples只是一个简单的 ...

  4. k8s yaml文件详解

    1.yaml格式的Pod配置文件内容及注解 深入Pod之前,首先我们来了解下Pod的yaml整体文件内容及功能注解. 如下: # yaml格式的pod定义文件完整内容: apiVersion: v1 ...

  5. 洛谷P3870 [TJOI2009]开关

    题目描述 现有\(N(2 ≤ N ≤ 100000)\)盏灯排成一排,从左到右依次编号为:\(1,2,......,N\).然后依次执行\(M(1 ≤ M ≤ 100000)\)项操作,操作分为两种: ...

  6. 关于JS的Date对象的探究

    一次代码审核,其中刚好遇见了一些知识点~记录顺带整理一下吧~ date对象是javascript语言中内置的数据类型,用于提供日期和时间的操作接口. Date类型使用自UTC1970年1月1日0点开始 ...

  7. 机器学习(四) SVM 支持向量机

    svr_linear = SVR('linear') #基于直线 svr_rbf = SVR('rbf') #基于半径 svr_poly = SVR('poly') #基于多项式

  8. 1089 Insert or Merge(25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  9. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...

  10. fiddle

    web开发中Chrome.IE.firefox等浏览器都自带提供了插件帮助开发者跟踪http数据,在手机客户端怎么实现http数据抓包呢?Fiddler可以实现真机调试抓包.Fiddler支持Any ...