Educational Codeforces Round 64 -B(贪心)
题目链接:https://codeforces.com/contest/1156/problem/B
题意:给一段字符串,通过变换顺序使得该字符串不包含为位置上相邻且在字母表上也相邻的情况,并输出。
思路:可以把该字符串的奇字符和偶字符按字典序分别组成字符串s1,s2,然后判断s1+s2和s2+s1是否满足条件,若都不满足则输出“No answer”。可以模拟一下,当s1+s2和s2+s1都不满足即最大奇字符和最小偶字符相差不为1且最大偶字符和最小奇字符相差不为1,是无法找到满足条件的字符串的。
AC代码:
#include<bits/stdc++.h>
using namespace std; int T,a[];
char s[]; int main(){
scanf("%d",&T);
while(T--){
char s1[],s2[];
int cnt1=,cnt2=;
memset(a,,sizeof(a));
scanf("%s",s);
int len=strlen(s);
for(int i=;i<len;++i)
++a[s[i]-'a'];
for(int i=;i<;i+=)
if(a[i]){
for(int j=;j<a[i];++j)
s1[cnt1++]='a'+i;
}
for(int i=;i<;i+=)
if(a[i]){
for(int j=;j<a[i];++j)
s2[cnt2++]='a'+i;
}
if(abs(s1[cnt1-]-s2[])!=){
for(int i=;i<cnt1;++i) printf("%c",s1[i]);
for(int i=;i<cnt2;++i) printf("%c",s2[i]);
printf("\n");
continue;
}
if(abs(s2[cnt2-]-s1[])!=){
for(int i=;i<cnt2;++i) printf("%c",s2[i]);
for(int i=;i<cnt1;++i) printf("%c",s1[i]);
printf("\n");
continue;
}
printf("No answer\n");
}
return ;
}
Educational Codeforces Round 64 -B(贪心)的更多相关文章
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- Educational Codeforces Round 64部分题解
Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...
- Educational Codeforces Round 64 部分题解
Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...
- Educational Codeforces Round 64(ECR64)
Educational Codeforces Round 64 CodeForces 1156A 题意:1代表圆,2代表正三角形,3代表正方形.给一个只含1,2,3的数列a,ai+1内接在ai内,求总 ...
- Educational Codeforces Round 64 (Rated for Div. 2) A,B,C,D,E,F
比赛链接: https://codeforces.com/contest/1156 A. Inscribed Figures 题意: 给出$n(2\leq n\leq 100)$个数,只含有1,2,3 ...
- Educational Codeforces Round 64 -C(二分)
题目链接:https://codeforces.com/contest/1156/problem/C 题意:给出n个数和整形数z,定义一对数为差>=z的数,且每个数最多和一个数组成对,求最多有多 ...
- Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)
题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n n个数,然后求有多少个区间[l,r] 满足 a[l]+a[r]=max([l, ...
- Educational Codeforces Round 64 (Rated for Div. 2)D(并查集,图)
#include<bits/stdc++.h>using namespace std;int f[2][200007],s[2][200007];//并查集,相邻点int find_(in ...
- Educational Codeforces Round 64 (Div. 2)
A.3*3讨论即可,注意正方形套圆套三角形只有6个点. #include<cstdio> #include<cstring> #include<iostream> ...
随机推荐
- 《转发》特征工程——categorical特征 和 continuous特征
from http://breezedeus.github.io/2014/11/15/breezedeus-feature-processing.html 请您移步原文观看,本文只供自己学习使用 连 ...
- 【leetcode】1232. Check If It Is a Straight Line
题目如下: You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coord ...
- 【leetcode】Department Top Three Salaries
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- 18.configparser模块
# 创建配置文件 import configparser config = configparser.ConfigParser() # 相当于config = {} 空字典 config[" ...
- C# 编辑
if (state == "编辑") { ht["修改人"] = Helper.用户名; ht["修改时间"] = DateTimeHelp ...
- jquery disabled选择器 语法
jquery disabled选择器 语法 作用:disabled 选择器选取所有禁用的表单元素.大理石平台价格表 语法:$(":disabled") jquery disable ...
- Java类路径的问题
下面是eclipse中的文件组织形式. 下面是硬盘中文件的组织形式: src:中就是自己编写的没有编译的代码. target中是编译的Java中的class文件和一些不用编译的文件.这样也就明白了为什 ...
- POJ 2195 一人一房 最小费用流 建图 水题
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21010 Accepted: 10614 Desc ...
- sh_09_字典的定义
sh_09_字典的定义 # 字典是一个无序的数据集合,使用print函数输出字典时,通常 # 输出的顺序和定义的顺序是不一致的! xiaoming = {"name": " ...
- win10 exe如何添加或禁用开机自启动项
一.添加开机自启动 1,先打开存放自启动软件文件的文件夹 方法①:在文件搜索框中输入或粘贴以下地址: C:\ProgramData\Microsoft\Windows\Start Menu\Progr ...