#C++初学记录(ACM试题1)
**A - Diverse Strings **
A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.
Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
Input
The first line contains integer n (1≤n≤100), denoting the number of strings to process. The following n lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 1 and 100, inclusive.
Output
Print n lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.
Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
Yes
Yes
Yes
Yes
No
No
No
No
正确代码
#include<cstdio>
#include<cstring>
int main(){
char a[105];
int t;
scanf("%d", &t);
while(t--){
memset(a, 0, sizeof(a));
scanf("%s", a);
int len = strlen(a), flag = 0;
for(int i = 0; i < len; i++){
for(int j = i+1; j < len; j++){
if(a[i] > a[j]){
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i = 1; i < len; i++){
if(a[i] - a[i-1] != 1){
printf("No\n");
flag = 1;
break;
}
}
if(flag == 1){
continue;
}else{
printf("Yes\n");
}
}
return 0;
}
题目理解
该题较为简单,该题的正确做法是对输入的字母进行判断,若有相近的字母则输出YES,若没有则输出NO,若输入的是单个字符则也输出YES。
相关知识点
%s是进行字符串的整体输入,即
int c[200];
scanf("%s",%c);
是将整个字符串拆分成单个字符储存进数组a,即输入abcde则a[1]=b。以及对单个字符进行判断,判断只需要在判断NO和YES是加一点小聪明.
for(int i = 1; i < len; i++){
if(a[i] - a[i-1] != 1){
printf("No\n");
flag = 1;
break;
}
}
if(flag == 1){
continue;
}else{
printf("Yes\n");
在这个if条件判断中,因为单个字符的长度len=1,所以在进行No的判断中是不符合的,因为No的判断条件是i=1;i<len;因此直接进行else循环。
#C++初学记录(ACM试题1)的更多相关文章
- #C++初学记录(ACM试题2)
Max Sum Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-seq ...
- #C++初学记录ACM补题(D. Candies!)前缀和运算。
D - Candies! Consider a sequence of digits of length [a1,a2,-,a]. We perform the following operati ...
- #C++初学记录(acm试题#预处理)
C - Lucky 7 in the Pocket BaoBao loves number 7 but hates number 4, so he refers to an integer as a ...
- #C++初学记录(set进阶#acm cf 190802 B. Subsegments)
B. Subsegments#set进阶 Programmer Sasha has recently begun to study data structures. His coach Stas to ...
- #C++初学记录(sort函数)
sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- javaweb初学记录
原文 链接 http://blog.csdn.net/iojust/article/details/52429805 - ---热情依旧 - 环境搭建: - jdk环境配置 jdk下载: http:/ ...
- #C++初学记录(算法4)
A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...
- 北大ACM试题分类+部分解题报告链接
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
随机推荐
- 23种设计模式之建造者模式(Builder)
建造者模式强调将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示.建造者模式是一步一步地创建一个复杂的对象,它允许用户只通过制定复杂对象的类型和内容就可以构建它们,用户不需要知道 ...
- Android 简单计算器实现源码
1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...
- windows网卡共享网络时,报Internet连接共享访问被启用时,出现了一个错误。(null)
今天笔者在自己电脑上作试验时,准备将无线网络连接共享给VMnet1时报Internet连接共享访问被启用时,出现了一个错误.(null)错误: 经上网查询到问题原因:没有启用 Windows Fire ...
- iOS 通过(lame)将录制音频转换成Mp3
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处,保留原帖地址及作者署名. Url:http://blog.csdn.net/ysy441088327/article/detail ...
- Edge Animate使用SPRITESHEET创建动画(三)
在Flash动画制作中,使用SpriteSheet制作动画元素是一个常见和普遍的方法.在Edge Animate中,我们也可以利用SpriteSheet来制作HTML5动画.本文将从一个示例出发,介绍 ...
- IDEA安全编码组件
import java.io.UnsupportedEncodingException;import java.security.Key;import java.security.Security; ...
- 关于IP地址子网的划分
- JAVA基础知识点转载
JAVA部分: 1.Java 指定线程执行顺序(三种方式) 转载link:https://blog.csdn.net/difffate/article/details/63684290 2.jdk7中 ...
- Django之ContentType详解
contenttypes 是Django内置的一个应用,可以追踪项目中所有app和model的对应关系,并记录在ContentType表中. models.py文件的表结构写好后,通过makemigr ...
- 找到多个与名为“xxx”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配的控制器,则会发生这种情况。
一次在建MVC 项目的进行开发的时候,因为后来想到了一个更好的项目名称,就把 Web项目的名称重命名 改了, 然后 程序集名称,默认命名空间,都改成新的了,刚建立的项目本身也不大,运行起来,总是报 & ...