跳过正文
  1. Posts/

codeforces 600 C. Make Palindrome

·2 分钟

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

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2015年12月08日 星期二 16时39分56秒
  4File Name :code/cf/problem/600C.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=2E5+7;
 34char st[N];
 35int len;
 36int cnt[30];
 37int odd[30];
 38int k;
 39int mappd[30];
 40char ans[N];
 41
 42void solve()
 43{
 44//  cout<<"k:"<<k<<endl;
 45    if (k==0)
 46    {
 47	for ( int i = 0 ; i < 26 ; i++)
 48	{
 49	    for ( int j = 0 ; j < cnt[i]/2 ; j++)
 50		printf("%c",char(i+'a'));
 51	}
 52	for ( int i = 25 ; i >= 0 ; i--)
 53	    for ( int j = 0 ; j < cnt[i]/2 ;  j++)
 54		printf("%c",char(i+'a'));
 55	return ;
 56    }
 57    if (k%2==0)
 58    {
 59	for ( int i = 1 ; i <= k/2 ; i++)
 60	{
 61	    cnt[odd[i]]++;
 62	    cnt[odd[i+k/2]]--;
 63	}
 64	int num =  0;
 65	for ( int i = 0 ; i < 26 ; i++)
 66	{
 67	    for ( int j = 0 ; j < cnt[i]/2; j++)
 68		printf("%c",char(i+'a'));
 69	}
 70	for ( int i = 25 ; i >=0 ; i--)
 71	{
 72	    for ( int j = 0 ; j < cnt[i]/2  ; j++)
 73		printf("%c",char(i+'a'));
 74	}
 75	printf("\n");
 76
 77    }
 78    if (k%2==1)
 79    {
 80//	cout<<"what the fuck?"<<endl;
 81	for ( int i = 1 ; i <= k/2 ; i++)
 82	{
 83	    cnt[odd[i]]++;
 84	    cnt[odd[i+k/2+1]]--;
 85	}
 86	int the_lonely_one = odd[k/2+1];
 87	int num = 0 ;
 88	for ( int i = 0 ; i < 26 ; i++)
 89	{
 90	    if (i==the_lonely_one ) cnt[i]--;
 91	    for ( int j = 0 ; j < cnt[i]/2 ; j++)
 92		printf("%c",char(i+'a'));
 93	}
 94//	puts("AAA??");
 95	printf("%c",char(the_lonely_one+'a'));
 96	for ( int i = 25 ; i >= 0 ; i--)
 97	{
 98	    for ( int j = 0 ; j < cnt[i]/2 ; j++)
 99		printf("%c",char(i+'a'));
100	}
101    }
102
103}
104
105int main()
106{
107	#ifndef  ONLINE_JUDGE
108//	freopen("code/in.txt","r",stdin);
109  #endif
110
111	scanf("%s",st);
112	len = strlen(st);
113	ms(cnt,0);
114	ms(odd,0);
115	for ( int i = 0 ; i   <  len ; i ++)
116	{
117	    cnt[st[i]-'a']++;
118	}
119	 k = 0;
120	for ( int i = 0 ; i < 26 ; i++)
121	{
122	    if (cnt[i]%2==1)
123	    {
124		k++;
125		odd[k] = i;
126	    }
127	}
128	solve();
129
130
131
132
133  #ifndef ONLINE_JUDGE
134  fclose(stdin);
135  #endif
136    return 0;
137}

相关文章

codeforces 482 A. Diverse Permutation(构造)

·1 分钟
C - C **Time Limit:**1000MS **Memory Limit:**262144KB 64bit IO Format:%I64d & %I64u Submit Status Description Permutation_p_ is an ordered set of integers _p_1, p_2, …, p__n, consisting of n distinct positive integers not larger than n. We’ll denote as_n the length of permutation _p_1, _p_2, …, p__n.

codeforces 158 B. Taxi

·1 分钟
http://codeforces.com/problemset/problem/158/B 题意:n组人,每组有si个(1<=si<=4),每辆车能装4个人。问最少需要多少辆车装下所有人并且保证同一组的人在一辆车里。 思路:统计人数分别为1,2,3,4的人数。对于4的直接加到答案。贪心的思路是:优先用人数少的去填人数多的。

codeforces 377 A maze

·2 分钟
http://codeforces.com/contest/377/problem/A 题意:给定一个n*m的maze. ‘.’代表空,‘#’代表墙。要求构造一种方案,使得将k个空格填成墙壁后不影响当前的连通性(即没有被填充的空格之间可以相互到达) 思路:一开始想从上往下从左往右构造。错误的认为四个角一定是可以变成墙的。

codeforces #334 div 2 B. More Cowbell

·1 分钟
题意是说。给n个balls,k个箱子。保证(n<=2*k) 一个箱子中中最多放两个balls,size为两个balls的size之和。 所有的箱子的size都要一样。 问size最小是多少。