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.
The only line contains string s (1 ≤ |s| ≤ 2·105) consisting of only lowercase Latin letters.
Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes.
aabc
abba
aabcd
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了,需要后面再优化下。)