Make Palindrome CodeForces - 600C(思维)
A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings "abb" and "ij" are not.
You are given string s consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in s. Then you can permute the order of letters as you want. Permutation doesn't count as changes.
You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically.
Input
The only line contains string s (1 ≤ |s| ≤ 2·105) consisting of only lowercase Latin letters.
Output
Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes.
Examples
aabc
abba
aabcd
abcba 就是用最小的改变次数把这个字符串 转换成回文串
可以移动字符的位置 不计入总次数
如果好几个次数相同的 取字典序最小的
统计每个单词的个数 从前 后分别 找到个数为奇数的单词 前面的++ 后边的--
如果只有一个 放到中间输出即可
原字符串长度为偶数的序列 不存在奇数个 个数奇数个的单词
#include <bits/stdc++.h>
using namespace std;
string str;
int num[];
vector<char> v;
int main()
{
cin>> str;
int len = str.size();
for(int i=; i<len; i++)
num[str[i]-'a']++;
int j = ;
int flag;
for(int i=; i<; i++)
{
if(num[i] & )
{
while(!(num[j] & ) && j >= i) j--;
if((num[j] & ) && j > i)
{
num[j]--;
num[i]++;
}
if(i == j)
flag = i;
}
if(num[i])
for(int k=; k<num[i]/; k++)
{
char tmp = 'a' + i;
printf("%c", tmp);
v.push_back(tmp);
}
}
if(len & )
printf("%c", 'a'+flag);
for(int i=v.size()-; i>=; i--)
cout<< v[i];
cout<< endl; return ;
}
Make Palindrome CodeForces - 600C(思维)的更多相关文章
- 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 ...
 - codeforces 600C  Make Palindrome
		
要保证变化次数最少就是出现次数为奇数的相互转化,而且对应字母只改变一次.保证字典序小就是字典序大的字母变成字典序小的字母. 长度n为偶数时候,次数为奇数的有偶数个,按照上面说的搞就好了. n为奇数时, ...
 - 【Codeforces 600C】Make Palindrome
		
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 计算出来每个字母出现的次数. 把字典序大的奇数出现次数的字母换成字典序小的奇数出现次数的字母贪心即可. 注意只有一个字母的情况 然后贪心地把字 ...
 - Codeforces 424A (思维题)
		
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
 - POJ2402/UVA 12050 Palindrome Numbers 数学思维
		
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...
 - 3-palindrome CodeForces - 805B (思维)
		
In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so ...
 - Codeforces 1060E(思维+贡献法)
		
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
 - Queue CodeForces - 353D (思维dp)
		
https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...
 - codeforces 1244C (思维 or 扩展欧几里得)
		
(点击此处查看原题) 题意分析 已知 n , p , w, d ,求x , y, z的值 ,他们的关系为: x + y + z = n x * w + y * d = p 思维法 当 y < w ...
 
随机推荐
- C#抽象类与抽象方法--就是类里面定义了函数而函数里面什么都没有做的类
			
看一下代码应该就可以了 using System; using System.Collections.Generic; using System.Linq; using System.Text; na ...
 - js 自己项目中几种打开或弹出页面的方法
			
自己项目中,几种打开或弹出页面的方法(部分需要特定环境下) var blnTop = false;//是否在顶层显示 ///动态生成模态窗体(通过字符串生成) ///strModalId:模态窗体ID ...
 - jqgrid 让隐藏的列在编辑状态时出现且可编辑
			
有时,我们需要隐藏一个列数据,但在启动编辑时又能够被编辑. 1.设置列为编辑:editable: true 2.设置 editrules属性值为: edithidden: true colModel: ...
 - Python爬虫爬取贴吧的帖子内容
			
最近在看一个大神的博客,从他那里学会了很多关于python爬虫的知识,其实python如果想用在实际应用中,你需要了解许多,比如正则表达式.引入库.过滤字段等等,下面不多说,我下面的程序是爬取Ubun ...
 - 使用php分页类实现简单分类
			
分页类參考地址:http://blog.csdn.net/buyingfei8888/article/details/40260127 just soso. 实现分页主要分为4步: 1 引入分页类 2 ...
 - HDFS--大数据应用的基石
			
近些年,由于智能手机的迅速普及推动移动互联网技术的蓬勃发展,全球数据呈现爆发式的增长.2018年5月企鹅号的统计结果:互联网每天新增的数据量达2.5*10^18字节,而全球90%的数据都是在过去的两年 ...
 - jq 在字符串中,去掉指定的元素
			
例: var arr= ["4.5岁", "3.5岁", "5.5岁", "5岁", "4岁"] v ...
 - mysql存储blob限制
			
一.Mysql存储类型分类: 1.blob:二进制大对象(字节流),可以用来存储图片.视频等,没有字符集的说法 2.text:文本大对象(字符流),存储大型字串,有字符集的说法 3.二者使用时不能指定 ...
 - 6、使用jconsole+VisualVM分析JVM
			
一.不断增加对象触发GC的代码 VM 参数:-Xms100m -Xmx100m -XX:+UseSerialGC import java.util.ArrayList; import java.uti ...
 - 问题解决:IDEA右键选择new新文件的时候没有JSP文件选项解决
			
参考: https://blog.csdn.net/tomorrow_fine/article/details/74090308 用上面的方法就可以解决了, 但是如果把web目录设置成了额外的,那id ...