codeforces 600C Make Palindrome
要保证变化次数最少就是出现次数为奇数的相互转化,而且对应字母只改变一次。保证字典序小就是字典序大的字母变成字典序小的字母。
长度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的更多相关文章
- 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 ...
- 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 ...
- 【Codeforces 600C】Make Palindrome
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 计算出来每个字母出现的次数. 把字典序大的奇数出现次数的字母换成字典序小的奇数出现次数的字母贪心即可. 注意只有一个字母的情况 然后贪心地把字 ...
- Codeforces Round 486C - Palindrome Transformation 贪心
C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...
- 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 ...
- codeforce 600C - Make Palindrome
练习string 最小变换次数下,且字典序最小输出回文串. #include <cstdio> #include <cstring> #include <cmath> ...
- CodeForces 600C【构造】
题意: 在原字符串中修改数量最少,然后保证最小字典序. #include <bits/stdc++.h> using namespace std; typedef long long LL ...
- 【Codeforces 486C】Palindrome Transformation
[链接] 我是链接,点我呀:) [题意] 光标一开始在p的位置 你可以用上下左右四个键位移动光标(左右)或者更改光标所在的字符(上下增加或减少ascill码) 问你最少要操作多少次才能使得字符串变成回 ...
- Codeforces 1304B. Longest Palindrome
根据数据范围,暴力可以解决,对每一个串,找与其互为回文的串,或者判断自身是否为回文串,然后两两将互为回文的串排列在头尾,中间放且只能最多放一个自身为回文串的串,因为题目说每个串都是不同的 #inclu ...
随机推荐
- [CentOS7] 增加yum源
下载最新rpm文件:http://fedoraproject.org/wiki/EPEL 通过源文件rpm来增加: rpm -ivh epel-release-latest-7.noarch.rpm
- JS中的for....in循环 和 for ...of循环以及iterable遍历Map和Set
for循环的一个变体是for ... in循环,它可以把一个对象的所有属性依次循环出来: var o = { name: 'Jack', age: 20, city: 'Beijing' }; for ...
- C 语言实例 - 创建各类三角形图案
C 语言实例 - 创建各类三角形图案 创建三角形图案. 实例 - 使用 * 号 #include <stdio.h> int main() { int i, j, rows; printf ...
- JS如何在本地读取json等文件
JS使用ajax等在本地读取文件的时候,会报如下的错误: 解决方法一: npm install http-server -g 全局安装 http-server 下载完成之后再在目标文件中cmd中输入 ...
- emmet缩写大全
Syntax Child: > nav>ul>li <nav> <ul> <li></li> </ul> </n ...
- Vue中全局导入和按需导入的区别
export {router} //按需导出 import {router} from './router' //按需导入路由模块 export default //全局导出store模块 store ...
- nagios客户端之nrpe3.2.1安装(Ubuntu)
1.删除dpkg安装的nrpedpkg -l | grep nrpedkpg -P nagios-nrpe-server 2.ubuntu下nrpe3.2.1安装 下载nrpe3.2.1的源码包:ht ...
- Codeforces 350D(计算几何)
要点 用A.B.C一般式确定每条直线 将合法的圆心中点存到每条直线所属的vector中 枚举所有线段,二分后\(O(1)\)得到其中存在多少答案,累加 #include <cstdio> ...
- 练习十三:水仙花数,用for循环实现
水仙花数是指一个n位正整数(n>=3),他得每个位上得数字得n次幂之和等于它本身(例如:1^3+5^3+3^3=153) for i in range(101,1000): #3位数得水仙花数 ...
- jquery选中checkbox
jquery选中checkbox: $(function(){ $("[value = bb]:checkbox").attr("checked", true) ...