Educational Codeforces Round 2 600C Make Palindrome(脑洞)

此博客介绍了如何通过最小化修改次数,将任意给定的字符串转换为回文串,并提供了AC代码实现。核心在于计算字符出现次数,特别关注奇数次字符,以决定是否及如何修改以形成回文。

C. Make Palindrome
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
aabc
output
abba
input
aabcd
output
abcba



题目链接: 点击打开链接

给出一个字符串, 可以更改任意位置字符或重新排序字符串使得字符串为回文, 要求变换最少次数, 输出变换后的字符串.

计数字符的个数, 遍历每个字符, 若当前字符为奇数, 则从后向前寻找个数为奇数的字符, 若找到的字符为本身, 则将当前字符放到字符串

中间, 否则替换为当前字符.

AC 代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 30;
string s;
int num[MAXN];
int main(int argc, char const *argv[])
{
	cin >> s;
	int len = s.size(), x = 25, pos = 0;
	for(int i = 0; i < len; ++i)
		num[s[i] - 'a']++;
	for(int i = 0; i < 26; ++i) {
		if(num[i] & 1) {
			while(num[x] % 2 == 0 && i < x) 
				x--;
			if(i == x) {
				num[i]--;
				s[len / 2] = i + 'a';
			}
			else {
				num[x]--;
				num[i]++;
			}
		}
		for(int j = 0; j < num[i] / 2; ++j) {
			s[pos] = i + 'a';
			s[len - pos - 1] = i + 'a';
			pos++;
		}
	}
	cout << s << endl;
	return 0;
}


### 关于Educational Codeforces Round 175 对于Div. 2参赛者的信息 针对编号为175的教育轮次比赛,在Codeforces平台上的此类赛事通常面向不同级别的编程爱好者开放,但有着特定的规定来区分参与者的分组。对于Div. 2的参与者而言,此级别通常是为那些评级低于2100的程序员准备的比赛环境[^1]。 值得注意的是,虽然提及到trusted participants的概念主要适用于第三级别的正式排名表单中的成员资格定义,即仅限参加了至少两个评分赛(每次比赛中解决了一个以上的问题),并且未曾达到过1900或更高的分数的选手才能成为受信任的第三级别成员;然而这一规定并不直接影响Div. 2参赛者的分类标准。 为了获取关于Educational Codeforces Round 175更具体的数据,比如确切的时间安排、题目列表以及特殊规则等细节,建议访问官方公告页面查看由主办方发布的最新消息和指南。这些资源能够提供最权威的第一手资料给有兴趣参加该活动的人士。 ```python # Python代码示例用于展示如何通过API查询比赛信息(假设存在这样的功能) import requests def get_contest_info(contest_id): url = f"https://codeforces.com/api/contest.standings?contestId={contest_id}" response = requests.get(url).json() if 'result' in response: contest_data = response['result']['contests'][0] return { "name": contest_data["name"], "startTimeSeconds": contest_data["startTimeSeconds"], "durationSeconds": contest_data["durationSeconds"] } else: return None print(get_contest_info(175)) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值