codeforce-600C. Make Palindrome(贪心)
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(贪心)的更多相关文章
- 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 ...
- codeforce 600C - Make Palindrome
练习string 最小变换次数下,且字典序最小输出回文串. #include <cstdio> #include <cstring> #include <cmath> ...
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- codeforces 600C Make Palindrome
要保证变化次数最少就是出现次数为奇数的相互转化,而且对应字母只改变一次.保证字典序小就是字典序大的字母变成字典序小的字母. 长度n为偶数时候,次数为奇数的有偶数个,按照上面说的搞就好了. n为奇数时, ...
- Educational Codeforces Round 2 C. Make Palindrome —— 贪心 + 回文串
题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds me ...
- SPOJ:The Next Palindrome(贪心&思维)
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- [CF1034B] Longest Palindrome - 贪心
如果自己是回文串可以做中心 如果一个串和另一个串的转置相等则可以凑一对 优先配对 #include <bits/stdc++.h> using namespace std; int n,m ...
- CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...
- Educational Codeforces Round 2
600A - Extract Numbers 20171106 字符串处理题,稍微注意点细节就能水过 #include<stdlib.h> #include<stdio.h&g ...
随机推荐
- 在 vscode.dev 中直接运行 Python !纯浏览器环境,无后端!
其实有挺长一段时间没有写自己的 VS Code 插件了! 还是要感谢我们 DevDiv 组的 Flexible Friday 活动,让我可以在工作日研究自己感兴趣的项目. Flexible Frida ...
- Kubernetes:应用自动扩容、收缩与稳定更新
在前面我们已经学习到了 Pod 的扩容.滚动更新等知识,我们可以手动为 Deployment 等设置 Pod 副本的数量,而这里会继续学习 关于 Pod 扩容.收缩 的规则,让 Pod 根据节点服务器 ...
- A Child's History of England.35
The other two clung to the yard for some hours. At length the young noble said faintly, 'I am exhaus ...
- 27.0 linux VM虚拟机IP问题
我的虚拟机是每次换一个不同的网络,b不同的ip,使用桥接模式就无法连接,就需要重新还原默认设置才行: 第一步:点击虚拟机中的编辑-->虚拟网络编辑器 第二步:点击更改设置以管理员权限进入 第三步 ...
- 大数据学习day18----第三阶段spark01--------0.前言(分布式运算框架的核心思想,MR与Spark的比较,spark可以怎么运行,spark提交到spark集群的方式)1. spark(standalone模式)的安装 2. Spark各个角色的功能 3.SparkShell的使用,spark编程入门(wordcount案例)
0.前言 0.1 分布式运算框架的核心思想(此处以MR运行在yarn上为例) 提交job时,resourcemanager(图中写成了master)会根据数据的量以及工作的复杂度,解析工作量,从而 ...
- 【leetcode】952. Largest Component Size by Common Factor(Union find)
You are given an integer array of unique positive integers nums. Consider the following graph: There ...
- 容器之分类与各种测试(四)——unordered-multiset
unordered-multiset是不定序关联式容器,其底部是通过哈希表实现功能. (ps:黑色框就是bucket,白色框即为bucket上挂载的元素) 为了提高查找效率,bucket(篮子)的数量 ...
- GO 定时器NewTimer、NewTicker使用
package main import ( "fmt" "sync" "time" ) /** *ticker只要定义完成,从此刻开始计时, ...
- mysql explain using filesort
创建表,字段tid上无索引(mysql 5.7) CREATE TABLE `test` ( `tid` int(11) DEFAULT NULL, `tname` varchar(12) DEFAU ...
- eclipse.ini配置 vmargs 说明
-vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M 1. 各个参数的含义什么? 参数中-vmargs的意思是设置JVM参数, ...