codeforces 600 C. Make Palindrome

http://codeforces.com/problemset/problem/600/C 题意:给定一个字符串。要求用最少的变换得到一个回文串。且在变换次数相同时要字典序最小的。输出变换后的字符串。 思路:对不能构成会文串有影响的是出现奇数次的字母。所以我们先统计每个字母出现的次数。然后按照出现奇数次的字母的个数分奇偶分别搞。偶数的话直接把后面一半变成前面一半。奇数的话,也是这样。输出的时候按照字母从a扫到z,如果有就输出一半。然后再倒着扫一遍。 输出另一半。这样可以保证是字典序最小。需要注意的是奇数的时候的输出情况。不要忘记中间那个字母。

/* ***********************************************
Author :111qqz
Created Time :2015年12月08日 星期二 16时39分56秒
File Name :code/cf/problem/600C.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=2E5+7;
 7char st[N];
 8int len;
 9int cnt[30];
10int odd[30];
11int k;
12int mappd[30];
13char ans[N];
 1void solve()
 2{
 3//  cout<<"k:"<<k<<endl;
 4    if (k==0)
 5    {
 6	for ( int i = 0 ; i < 26 ; i++)
 7	{
 8	    for ( int j = 0 ; j < cnt[i]/2 ; j++)
 9		printf("%c",char(i+'a'));
10	}
11	for ( int i = 25 ; i >= 0 ; i--)
12	    for ( int j = 0 ; j < cnt[i]/2 ;  j++)
13		printf("%c",char(i+'a'));
14	return ;
15    }
16    if (k%2==0)
17    {
18	for ( int i = 1 ; i <= k/2 ; i++)
19	{
20	    cnt[odd[i]]++;
21	    cnt[odd[i+k/2]]--;
22	}
23	int num =  0;
24	for ( int i = 0 ; i < 26 ; i++)
25	{
26	    for ( int j = 0 ; j < cnt[i]/2; j++)
27		printf("%c",char(i+'a'));
28	}
29	for ( int i = 25 ; i >=0 ; i--)
30	{
31	    for ( int j = 0 ; j < cnt[i]/2  ; j++)
32		printf("%c",char(i+'a'));
33	}
34	printf("\n");
 1    }
 2    if (k%2==1)
 3    {
 4//	cout<<"what the fuck?"<<endl;
 5	for ( int i = 1 ; i <= k/2 ; i++)
 6	{
 7	    cnt[odd[i]]++;
 8	    cnt[odd[i+k/2+1]]--;
 9	}
10	int the_lonely_one = odd[k/2+1]; 
11	int num = 0 ;
12	for ( int i = 0 ; i < 26 ; i++)
13	{
14	    if (i==the_lonely_one ) cnt[i]--;
15	    for ( int j = 0 ; j < cnt[i]/2 ; j++)
16		printf("%c",char(i+'a'));
17	}
18//	puts("AAA??");
19	printf("%c",char(the_lonely_one+'a'));
20	for ( int i = 25 ; i >= 0 ; i--)
21	{
22	    for ( int j = 0 ; j < cnt[i]/2 ; j++)
23		printf("%c",char(i+'a'));
24	}
25    }
}
1int main()
2{
3	#ifndef  ONLINE_JUDGE 
4//	freopen("code/in.txt","r",stdin);
5  #endif
 1	scanf("%s",st);
 2	len = strlen(st);
 3	ms(cnt,0);
 4	ms(odd,0);
 5	for ( int i = 0 ; i   <  len ; i ++)
 6	{
 7	    cnt[st[i]-'a']++;
 8	}
 9	 k = 0;
10	for ( int i = 0 ; i < 26 ; i++)
11	{
12	    if (cnt[i]%2==1)
13	    {
14		k++;
15		odd[k] = i;
16	    }
17	}
18	solve();
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}