#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 ...
随机推荐
- 使用COSBench工具对ceph s3接口进行压力测试
一.COSBench安装 COSBench是Intel团队基于java开发,对云存储的测试工具,全称是Cloud object Storage Bench 吐槽下,貌似这套工具是intel上海团队开发 ...
- javaweb分页的后端实现
先上demo图 servlet实现部分: package servlet; import java.io.IOException; import java.util.List; import java ...
- thinkCMF----列表页跳转
thinkCMF列表循环有个:用来循环文章列表. <php> $where=[ 'post.create_time'=>['egt',0] ]; $page=[ 'list_rows ...
- Spring Framework框架容器核心源码逐步剖析
目录 构建Spring环境 Spring 版本 5.1.3.RELEASE 测试类 Spring 配置文件 测试方法Main 快速进入Debug查看IOC容器构建源码 Spring IOC源码步骤分析 ...
- QrenCode : 命令行下生成二维码图片
对于二维码大家应该并不陌生,英文名为 2-dimensional bar code 或 QR Code,是一种用图形记载信息的技术,最常见的是应用在手机应用上.用户通过手机摄像头扫描二维码或输入二维码 ...
- 天梯赛 大区赛 L3-014.周游世界 (Dijkstra)
L3-014. 周游世界 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 周游世界是件浪漫事,但规划旅行路线就不一定了-- 全世 ...
- AIX 7命令行weblogic建域流水
$ ./config.shUnable to instantiate GUI, defaulting to console mode. <------------------- Fusion M ...
- Oracle体系结构之OFM管理
OMF:oracle management files 作用:不用指定文件的路径大小名字 OMF管理数据文件:db_create_file_dest 传统方式:SQL>create tables ...
- Master Boot Record
https://en.wikipedia.org/wiki/Master_boot_record https://zh.wikipedia.org/wiki/主引导记录 A master boot r ...
- C语言概述
打印摄氏度 /* 1.1 使用int类型进行计算 */ #include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, ...