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。注意奇数的特殊处理。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <stack> #include <bitset> #include <queue> #include <set> #include <map> #include <string> #include <algorithm> #define Si(a) scanf("%d",&a) #define Sl(a) scanf("%lld",&a) #define Sd(a) scanf("%lf",&a) #define Ss(a) scanf("%s",a) #define Pi(a) printf("%d\n",(a)) #define Pl(a) printf("%lld\n",(a)) #define Pd(a) printf("%lf\n",(a)) #define Ps(a) printf("%s\n",(a)) #define W(a) while(a--) #define mem(a,b) memset(a,(b),sizeof(a)) #define FOP freopen("data.txt","r",stdin) #define inf 0x3f3f3f3f #define maxn 200010 #define mod 1000000007 #define PI acos(-1.0) #define LL long long using namespace std; char a[maxn]; int v[200]; struct node { char alp; int pos; } v2[200]; int main() { //FOP; Ss(a); int len=strlen(a); sort(a,a+len); int i,j,fi,top=0; char alp1; mem(v,0); mem(&v2,0); for(i=0; i<len; i++) { v[a[i]]++; } for(i='a'; i<='z'; i++) { if(v[i]%2) { v2[top].alp=i; v2[top].pos=v[i]; top++; } } for(i=top-1,j=0;i>=top/2;i--,j++) { v[v2[i].alp]--; v[v2[j].alp]++; } alp1=v2[top/2].alp; for(i='a'; i<='z'; i++) { for(j=0; j<v[i]/2; j++) { printf("%c",i); } } if(len%2) { printf("%c",alp1); } for(i='z'; i>='a'; i--) { for(j=0; j<v[i]/2; j++) { printf("%c",i); } } return 0; }