2018.1.22【CodeForces - 600C】解题报告(贪心,字符串)

本文介绍了一种构造最短回文串的方法,通过贪心算法实现字符串转换,确保改动次数最少并保持字典序最小。

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.

Examples
input
aabc
output
abba
input
aabcd
output
abcba

【题目大意】

任意输入一串字符,要求输出改动次数最少(移动交换位置不算做改动)且字典序最小的回文串。

【解题思路】

贪心的想法还是比较直观的。由于移动交换位置不算改动。贪心的从两端对称输出典序最小的字母,如果该字母出现次数位奇数,则从字典序最大的字母开始搜索,直至找到第一个出现个数也为奇数的字母(当然也是最大的),大字母的个数-1,小字母个数+1(即用小替换大),偶数个数会调整全部字母出现次数为偶数然后结束,奇数个数的话则需要额外判断,如果搜索到的字母和原字母相同,则说明该字母是中间字母(不一定该字母都在中间!)

易错:把中间字母单独提出来然后相邻的放到中间。比如aabbhhwwhwwhhbbaa输入本应该原样输出,但是却输出了aabbwwhhhhhwwbbaa,这样会使字典序增大,需要对中间字母特殊处理(正常左右两端输出,只不过最后留一个在中间打印就行。)

【解题代码】

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 200100
using namespace std;
char s[maxn];
char outs[maxn];
//int alpha[26];
bool mark[26];
struct node
{
	char letter;
	int times;
}alpha[26];
bool cmp(struct node n1,struct node n2)
{
	return n1.letter>n2.letter;
}
bool cmp1(struct node n1,struct node n2)
{
	return n1.letter<n2.letter;
}
void odd(int len)
{
//	printf("in odd\n");
	for(int i=0;i<len;i++)
	{
		alpha[s[i]-97].times++;
	}
	sort(alpha,alpha+26,cmp);
//	for(int i=0;i<26;i++)
//	{
//		printf("%d  %c %d\n",i,alpha[i].letter,alpha[i].times);
//	}

	int point=0,target=0;
	for(int i=0;i<26;i++)
	{
		if(!(alpha[i].times%2)) continue;
		else
		{
			target=i;
			int j;
			for(j=target,point=0;j<26;j++)
			{
				if(!(alpha[j].times%2)) continue;
				else point=j;
			}
			if(point==0) break;
			else
			{
				alpha[point].times++;
				alpha[target].times--;
			}
		}
	}
//	for(int i=0;i<26;i++)
//	{
//		printf("%d  %c %d\n",i,alpha[i].letter,alpha[i].times);
//	}
	char c=alpha[target].letter;
	char t=alpha[target].times;
//	printf("target=%c\n",c);
//	target 中间单值
	int count=0;
	int leftlen=len-alpha[target].times;
	sort(alpha,alpha+26,cmp1);
//	printf("now len=%d\n",leftlen);
	for(int i=0;count<(len-1)/2;i++)
	{
		if(alpha[i].letter==c) 
			for(int j=0;j<(alpha[i].times-1)/2;j++)
				outs[count++]=alpha[i].letter;
		else for(int j=0;j<alpha[i].times/2;j++)
				outs[count++]=alpha[i].letter;
	}
	int i;
	for(i=0;i<count;i++)
	{
		printf("%c",outs[i]);
	}
	printf("%c",c);
	for(i=count-1;i>=0;i--)
	{
		printf("%c",outs[i]);
	}
	printf("\n");
}
void even(int len)
{
//	printf("in even\n");
	for(int i=0;i<len;i++)
	{
		alpha[s[i]-97].times++;
	}
	sort(alpha,alpha+26,cmp);
//	for(int i=0;i<26;i++)
//	{
//		printf("%d  %c %d\n",i,alpha[i].letter,alpha[i].times);
//	}

	int point=0,target=0;
	for(int i=0;i<26;i++)
	{
//				if(i==25 ) printf("YOO\n");
		if(!(alpha[i].times%2)) continue;
		else
		{
			target=i;
			int j;
			for(j=target,point=0;j<26;j++)
			{
				if(!(alpha[j].times%2)) continue;
				else point=j;
			}
			if(point==0) break;
			else
			{
				alpha[point].times++;
				alpha[target].times--;
			}
		}
	}
//	char c=alpha[target].letter;
//	char t=alpha[target].times;
//	printf("target=%c\n",c);
//	//target 中间单值
	int count=0;
//	int leftlen=len-alpha[target].times;
	sort(alpha,alpha+26,cmp1);
//	printf("now len=%d\n",leftlen);
	for(int i=0;count<len/2;i++)
	{
		for(int j=0;j<alpha[i].times/2;j++)
			outs[count++]=alpha[i].letter;
	}
	int i;
	for(i=0;i<count;i++)
	{
		printf("%c",outs[i]);
	}
	for(i=count-1;i>=0;i--)
	{
		printf("%c",outs[i]);
	}
	printf("\n");
}
int main()
{
//			freopen("in.txt", "r", stdin);//其实in.txt是可以修改成其他名字的  比如“输入.txt”,都是可以的,这里只是为了方便起见,下同; 
//    freopen("outmy.txt", "w", stdout);
	while(~scanf("%s",s))
	{
		memset(alpha,0,sizeof(alpha));
		for(int i=0;i<26;i++)
		{
			alpha[i].letter='a'+i;
			alpha[i].times=0;
		}
		int len=strlen(s);
		if(len%2)//Odd
			odd(len);
		else even(len); 
	}
	return 0;
}


【收获与反思】

(第一次写贪心,由于开始理解有偏差,改了很多次,代码也很丑陋= =而且太冗长= =,好在最后AC了,需要后面再优化下。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值