CodeForces - 600C Make Palindrome 贪心
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 ins. 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.
Example
aabc
abba
aabcd
abcba
题目大意:输入一串字符串,改变其中的字母或调整顺序,最后使得输出的字符串为字典序最小的回文,且要求变换次数最小(调准顺序不算)
#include<stdio.h>
#include<string.h>
char str[200000];
int main()
{
int n = 0;
int re[26] = { 0 }, ch[26] = {0}; int len;
scanf("%s",str);
len = strlen(str);
for (int i = 0; i < len; i++)
{
re[str[i]-'a']++;
}
for (int i = 0; i < 26; i++)
{
if (re[i] % 2 != 0)
{
ch[n++] = i;
}
}
for (int i = 0, j = n-1; i < j; i++, j--)
{
re[ch[i]]++;
re[ch[j]]--;
}
if (n % 2 != 0)
{
str[len / 2] = ch[n / 2] + 'a';
}
for (int i = 0, j = len-1,h=0; i <= j; i++, j--)
{
for (; h < 26; h++)
{
if (re[h] >= 2)
{
re[h] -= 2;
str[i] = h+'a';
str[j] = h+'a';
break;
}
}
}
printf("%s", str); return 0;
}
CodeForces - 600C Make Palindrome 贪心的更多相关文章
- codeforces 600C Make Palindrome
要保证变化次数最少就是出现次数为奇数的相互转化,而且对应字母只改变一次.保证字典序小就是字典序大的字母变成字典序小的字母. 长度n为偶数时候,次数为奇数的有偶数个,按照上面说的搞就好了. n为奇数时, ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- CodeForces - 50A Domino piling (贪心+递归)
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...
- 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 Round 486C - Palindrome Transformation 贪心
C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...
- 【Codeforces 600C】Make Palindrome
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 计算出来每个字母出现的次数. 把字典序大的奇数出现次数的字母换成字典序小的奇数出现次数的字母贪心即可. 注意只有一个字母的情况 然后贪心地把字 ...
- CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...
随机推荐
- 多角度看.NET面试题
1.ASP.NET中的身份验证有那些?你当前项目采用什么方式验证请解释 身份验证是从用户获取名称和密码等标识凭证并根据某些机构验证这些凭据的过程.如果凭据有效,则提交该凭据的实体被视为通 ...
- 判断html是否含有图片
核心代码: $url="http://XXXXX/article/012.html"; $content=file_get_contents($url); //读取文章页面源代码 ...
- 天梯赛 L2-022. (数组模拟链表) 重排链表
题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...
- 61.volatile关键字
volatile作用 volatile的作用是可以保持共享变量的可见性,即一个线程修改一个共享变量后,另一个线程能够读取到这个修改后的值. 先来看一个问题: 定义一个Task类 package com ...
- align-items和align-content的区别
最近在研究flex布局,容器中有两个属性,是用来定义crossAxis测轴排列方式的.一开始接触align-items还可以理解感觉不难,后来看到align-content就感觉有点混淆了,特开一篇博 ...
- 关于Python编码问题小记
Python编码问题小记: 引子: 最近在复习redis,当我在获取redis的key的时候,redis 存储英文和汉字下面这个样子的,我知道汉字是用16进制的UTF-8编码了,然后突然很想搞清楚字符 ...
- 如何清理休眠文件(hiberfil.sys)
如果使用了休眠功能,那么打开系统盘就会有一个很大(5.36G)的hiberfil.sys文件,它是将用户正在运行的程序,保存在这里,再启动系统就很快了.如要清理它(不用休眠功能,或者临时腾出空间),可 ...
- cin.get()和cin.getline()之间的区别
cin.getline()和cin.get()都是对输入的面向行的读取,即一次读取整行而不是单个数字或字符,但是二者有一定的区别. cin.get()每次读取一整行并把由Enter键生成的换行符留在输 ...
- Interger不可变原理
1.先看代码: package main.java.db.mq; public class TestSwap { public static void main(String[] args) { In ...
- Vue 3.0 的生命周期
new Vue() new一个vue实例化对象 init Event & Lifecycle 执行一些初始化和生命周期相关的操作 beforeCreate 组件实例刚刚被创建出来 执行一些初始 ...