http://codeforces.com/problemset/problem/600/C

题意:给你一个小写字母组成的英文串,将它转换为回文串,要求,改变的字母的个数最小,移动字母不算改变字母。

所得的串字典序是最小的。最后输出所得到的串。

思路:要求改变的字母数最小那么用贪心的思想,就把原来的字母尽可能多的填入要求的串中。

首先,先把原串中的字母统计出来,开个数组存对应的字符的个数,然后从‘a’开始循环,如果对应字母的个数大于1;

如果是偶数个的话,就在所求串两端一边加一个,可以正好加完,若是奇数个的话那么按这样的操作,最后就剩下一个,那么把它加入队列。

最后操作队列中的单个的,然后补一个加到串的两端,直到串被补满。

然后再对串的一半排下序就可以了。

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 int cmp(const void*p,const void*q);
6 char a[100005*2];
7 char b[100005*2];
8 char c[100005*2];
9 char bb[100005*2];
10 int aa[26];
11 #include<queue>
12 using namespace std;
13 int main(void)
14 {
15 int n,i,j,k,p,q,l,z;
16 while(scanf("%s",a)!=EOF)
17 {
18 queue<int>que;
19 z=0;
20 memset(aa,0,sizeof(aa));
21 l=strlen(a);
22 for(i=0; i<l; i++)//统计对应的字母有多少个
23 {
24 aa[a[i]-'a']++;
25 }
26 int t=0;
27 for(i=0; i<26; i++)
28 {
29 if(aa[i]!=0)
30 {
31 if(aa[i]>=2)//大于2的先加在串的两端
32 {
33 while(aa[i]>1)
34 {
35 aa[i]-=2;
36 a[t]=i+'a';
37 a[l-1-t]=i+'a';
38 t++;
39 z+=2;
40 }
41 }
42 if(aa[i]==1) que.push(i);//剩下1的入队
43
44 }
45
46
47 }
48 while(!que.empty())
49 {
50 int f=que.front();
51 que.pop();
52 if(z>=l)
53 {
54 break;
55 }
56 while(aa[f]>0)
57 {
58 aa[f]-=2;
59 a[t]=f+'a';
60 a[l-1-t]=f+'a';
61 t++;
62 z+=2;
63 if(z>l)
64 {
65 break;
66 }
67 }
68 if(z>=l)//当满了就跳出
69 {
70 break;
71 }
72 }
73 int uu;
74 if(l%2==0)//找串的一半(分奇数偶数讨论)
75 {
76 uu=l/2;
77 }
78 else uu=(l-1)/2;
79 for(i=0; i<uu; i++)
80 {
81 b[i]=a[i];
82 }
83 qsort(b,uu,sizeof(char),cmp);//对串的一半排序
84 for(i=0; i<uu; i++)
85 {
86 printf("%c",b[i]);
87 }
88 if(l%2==1)
89 {
90 printf("%c",a[(l)/2]);
91 }
92 for(i=uu-1; i>=0; i--)
93 {
94 printf("%c",b[i]);
95 }
96 printf("\n");
97
98 }
99 return 0;
100 }
101 int cmp(const void*p,const void*q)
102 {
103 char *w=(char*)p;
104 char *u=(char*)q;
105 return (*w-'a')-(*u-'a');
106 }

codeforce-600C. Make Palindrome(贪心)的更多相关文章

  1. CodeForces - 600C Make Palindrome 贪心

    A string is called palindrome if it reads the same from left to right and from right to left. For ex ...

  2. codeforce 600C - Make Palindrome

    练习string 最小变换次数下,且字典序最小输出回文串. #include <cstdio> #include <cstring> #include <cmath> ...

  3. Educational Codeforces Round 2 C. Make Palindrome 贪心

    C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  4. codeforces 600C Make Palindrome

    要保证变化次数最少就是出现次数为奇数的相互转化,而且对应字母只改变一次.保证字典序小就是字典序大的字母变成字典序小的字母. 长度n为偶数时候,次数为奇数的有偶数个,按照上面说的搞就好了. n为奇数时, ...

  5. Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串

    题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...

  6. SPOJ:The Next Palindrome(贪心&思维)

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  7. [CF1034B] Longest Palindrome - 贪心

    如果自己是回文串可以做中心 如果一个串和另一个串的转置相等则可以凑一对 优先配对 #include <bits/stdc++.h> using namespace std; int n,m ...

  8. CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)

    题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...

  9. Educational Codeforces Round 2

    600A - Extract Numbers    20171106 字符串处理题,稍微注意点细节就能水过 #include<stdlib.h> #include<stdio.h&g ...

随机推荐

  1. 类成员函数调用delete this会发生什么呢?

    有如下代码 class myClass { public: myClass(){}; ~myClass(){}; void foo() { delete this; } }; int main() { ...

  2. 🚀 RabbitMQ课程发布-KuangStudy

    RabbitMQ课程上线(44集) 视频教程地址:https://www.kuangstudy.com/course/detail/1323452886432944129 专栏地址:https://w ...

  3. day04 Linux基础命令

    day04 Linux基础命令 查看帮助信息命令 1.man命令:man命令的功能是查看指定命令的详细解释. 格式:man [具体需要被查看的命令] [root@localhost ~]# man r ...

  4. Flume(三)【进阶】

    [toc] 一.Flume 数据传输流程 重要组件: 1)Channel选择器(ChannelSelector) ​ ChannelSelector的作用就是选出Event将要被发往哪个Channel ...

  5. Hadoop【MR开发规范、序列化】

    Hadoop[MR开发规范.序列化] 目录 Hadoop[MR开发规范.序列化] 一.MapReduce编程规范 1.Mapper阶段 2.Reducer阶段 3.Driver阶段 二.WordCou ...

  6. Docker学习(一)——安装docker

    Suse12上安装docker   对于suse13.2之后的版本,因为docker已经被添加到了suse仓库中,直接使用sudo zypper install docker即可.   suse12不 ...

  7. 队列——Java实现

    1 package struct; 2 3 interface IQueue{ 4 //入队列 5 void add(Object obj); 6 //出队列 7 Object remove(); 8 ...

  8. Spring Cloud中使用Eureka

    一.创建00-eurekaserver-8000 (1)创建工程 创建一个Spring Initializr工程,命名为00-eurekaserver-8000,仅导入Eureka Server依赖即 ...

  9. Spring是如何保证同一事务获取同一个Connection的?使用Spring的事务同步机制解决:数据库刚插入的记录却查询不到的问题(转)

    前言 关于Spring的事务,它是Spring Framework中极其重要的一块.前面用了大量的篇幅从应用层面.原理层面进行了比较全方位的一个讲解.但是因为它过于重要,所以本文继续做补充内容:Spr ...

  10. JDBCUtils工具类的属性

    package cn.itcast.util;import java.io.FileReader;import java.io.IOException;import java.net.URL;impo ...