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

原始题目

C. Make Palindrome

  • time limit per test 2 seconds
  • memory limit per test 256 megabytes
  • input standard input
  • output standard output

Problem Description

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,这样会使字典序增大,需要对中间字母特殊处理(正常左右两端输出,只不过最后留一个在中间打印就行。)

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#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了,需要后面再优化下。)