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 ...
随机推荐
- MVC 全局拦截aciton
上篇:MVC 拦截指定的action 有时,我们需要对所有aciton在执行前/后做一些(预)处理,如何实现呢? 1.定义一个action筛选类.继承至System.Web.Mvc.IActionFi ...
- MariaDB数据库性能优化
1. 硬件优化 1.1 内存(Memory) 内存是最重要的因素,因为它允许您调整服务器系统变量.更多的内存意味着可以将更大的密钥和表缓存存储在内存中,从而减少磁盘访问速度,降低一个数量级. 如果未将 ...
- 2017-2018-2 20155203《网络对抗技术》 Exp7:网络欺诈防范
1.基础问题回答 (1)通常在什么场景下容易受到DNS spoof攻击 连接无线网络,和恶意攻击者处在同一局域网下. (2)在日常生活工作中如何防范以上两攻击方法 首先决不去点击浏览器都认为不安全的网 ...
- 20155218《网络对抗》MSF基础应用
20155218<网络对抗>MSF基础应用 实验过程 1.一个主动攻击实践,如ms08_067; 首先使用 search ms08_067查询一下该漏洞: show target 查看可以 ...
- 在ListBoxItem的样式中的button传参,把当前选中项传递到命令的方法
原文:在ListBoxItem的样式中的button传参,把当前选中项传递到命令的方法 前端页面: <Style x:Key="ThumbItemStyle" TargetT ...
- Asp.Net StateServer实现共同域名下Session共享
概述 在实验的时候,参照了多方的信息,确实成功了,这里简单记录一下. 我们知道,在Asp.Net的Web.Config文件中,System.Web节点下,有个sessionState节点,它说明了应用 ...
- 汇编 XOR运算
XOR运算 按位异或^ 一.按位异或^ 运算符^ 1^1=0;0^0=0; //相同则为0 0^1=1;1^0=1; //不相同为1 1101^0110=1011; // asm_XOR.c ...
- opengl学习记录1——矩形绘制
#include <windows.h> #include <gl/GL.h> #include <gl/GLU.h> #include <glut.h> ...
- effective c++ 笔记 (13-17)
//---------------------------15/03/30---------------------------- //#13 以对象管理资源 { void f() { Inves ...
- 阿里(蚂蚁,菜鸟)+百度+网易面经(JAVA)
本人综述: 本人本科和硕士均是毕业于普通的双非院校,参加过ACM竞赛,阿里面了三次全被拒了,网易收到offer. 阿里面试: 阿里的面试问的都是很基础的东西,如操作系统的简单问题,计算机网络的基本问题 ...