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. ProxyApi-大数据采集用的IP代理池

    用于大数据采集用的代理池 在数据采集的过程中,最需要的就是一直变化的代理ip. 自建adsl为问题是只有一个区域的IP. 买的代理存在的问题是不稳定,影响采集效率. 云vps不允许安装花生壳等,即使有 ...

  2. SpringCloud微服务实战——搭建企业级开发框架(二十八):扩展MybatisPlus插件DataPermissionInterceptor实现数据权限控制

    一套完整的系统权限需要支持功能权限和数据权限,前面介绍了系统通过RBAC的权限模型来实现功能的权限控制,这里我们来介绍,通过扩展Mybatis-Plus的插件DataPermissionInterce ...

  3. MapReduce08 数据清洗(ETL)和压缩

    目录 数据清洗(ETL) ETL清洗案例 需求 需求分析 实现代码 编写WebLogMapper类 编写WebLogDriver类 打包到集群运行 压缩 概念 MR支持的压缩编码 压缩算法对比 压缩性 ...

  4. nextcloud搭建私有云盘

    一.基础环境准备 1.安装一台centos7的linux服务器. # 系统初始化 # 如果时区不对,请修改时区 #mv /etc/localtime /etc/localtime_bak #ln -s ...

  5. myatoi

    atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中.int atoi(const char *nptr) 函数会扫描参数 nptr字符串 ...

  6. Android 图片框架

    1.图片框架:Picasso.Glide.Fresco 2.介绍: picasso:和Square的网络库能发挥最大作用,因为Picasso可以选择将网络请求的缓存部分交给了okhttp实现 Glid ...

  7. ES6常用的数值转换方法

    <script type="text/javascript"> // Number常用方法 /* Number.isFinite() 用来检查一个数值是否为有限的(fi ...

  8. 【Java 与数据库】How to Timeout JDBC Queries

    How to Timeout JDBC Queries JDBC queries by default do not have any timeout, which means that a quer ...

  9. linux重启后JDk环境变量配置失效最终解决方案

    最终解决方案:https://bbs.deepin.org/forum.php?mod=viewthread&tid=147762 其实这个修改可能也存在问题,如果有耐心的可以每次打开终端   ...

  10. [BUUCTF]PWN——hitcontraining_heapcreator

    hitcontraining_heapcreator 附件 步骤: 例行检查,64位程序,开启了canary和nx 本地试运行一下,看看大概的情况,经典的堆的菜单 64位ida载入,main函数没有什 ...