题目链接: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(贪心)的更多相关文章

  1. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  2. Educational Codeforces Round 64部分题解

    Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...

  3. Educational Codeforces Round 64 部分题解

    Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...

  4. Educational Codeforces Round 64(ECR64)

    Educational Codeforces Round 64 CodeForces 1156A 题意:1代表圆,2代表正三角形,3代表正方形.给一个只含1,2,3的数列a,ai+1内接在ai内,求总 ...

  5. 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 ...

  6. Educational Codeforces Round 64 -C(二分)

    题目链接:https://codeforces.com/contest/1156/problem/C 题意:给出n个数和整形数z,定义一对数为差>=z的数,且每个数最多和一个数组成对,求最多有多 ...

  7. 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, ...

  8. 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 ...

  9. Educational Codeforces Round 64 (Div. 2)

    A.3*3讨论即可,注意正方形套圆套三角形只有6个点. #include<cstdio> #include<cstring> #include<iostream> ...

随机推荐

  1. HandlerAdapter解析参数过程之HandlerMethodArgumentResolver

    在我们做Web开发的时候,会提交各种数据格式的请求,而我们的后台也会有相应的参数处理方式.SpringMVC就为我们提供了一系列的参数解析器,不管你是要获取Cookie中的值,Header中的值,JS ...

  2. A. Detective Book

    A. Detective Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. A. Transmigration

    A. Transmigration time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. git多人参与的项目 -> 分支代码如何合并到主干

    个人理解:合并分支时候就是当前分支,与别的分支先合并一遍,然后解决分支中存在的所有冲突,之后将本地分支代码提交到git远程仓库,之后切换主干分支 ,将主干分支与分支内容合并,解决冲突, 在提交主干分支 ...

  5. 整合spring之后,struts2里面的自定义拦截器的invocation.invoke()总是返回input

    这个真的是整死我了,还好看见了一篇博客提示了我, 解决方法: 在spring的bean配置中我没有设置action的作用域为prototype,也就是多例的,如果不设置则就会是默认的singleton ...

  6. Java期末课程学习汇总。

    本学期面向对象与Java程序设计课程已经结束了,给自己学习来个总结. 本学期过的非常快,不得不说这一学期学到的东西很少,感觉自己的进步很小. 而且感觉自己总少了点什么,在写这篇总结前,我认真想了,很多 ...

  7. Note: Bigtable, A Distributed Storage System for Structured Data

    Abstract Introduction::  Bigtable设计主旨:可扩地扩展到pByte级别和数千台机器的系统, 通用.可伸缩.高性能.高可用性.  不实现完整的关系数据模型,而是支持一个可 ...

  8. CRC32算法C#中的实现

    代码如下: using System; using System.Collections.Generic; using System.Text; using System.IO; namespace ...

  9. LaTex&&markdown

    LaTeX在线编辑器:传送门 LaTeX常用公式整理(转载):传送门 Markdown洛谷教程(转载):传送门 Markdown基本语法(转载):传送门 Markdown数学符号和语法(转载):传送门 ...

  10. 阶段3 2.Spring_04.Spring的常用注解_6 用于注入数据的注解

    @Qualifier 指定id为accountDao1 测试 如果把Autowired注释掉的话, 就会报空指针异常.就是没有注入进来 @Qualifier不能独立使用必须和Autowired配合使用 ...