#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 ...
随机推荐
- requests源码分析
0.前言 (1) 拆部分reques中感兴趣t的轮子 (2)对一些感兴趣的pythonic写法做一些归纳 1.用object.__setattr__来初始化构造函数 反正我之前就是直接实例对象时把所有 ...
- 3944: Sum[杜教筛]
3944: Sum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3471 Solved: 946[Submit][Status][Discuss] ...
- 安装sqlite3.8的方法
wget http://www.sqlite.org/2015/sqlite-autoconf-3081101.tar.gz tar -xvzf sqlite-autoconf-3081101.tar ...
- cadence allegro 布线时添加过孔
1.在放置过孔前先要进行简单的设置. 在菜单栏Setup->Constraints->physical出来的列表里面找到vias 点击出现一个对话框在对话框中选择需要的过孔.(类型比较多可 ...
- Unity3D笔记 GUI 四、实现选项卡三
一.代码: using UnityEngine; using System.Collections; /// <summary> /// 选项卡二 /// </summary> ...
- CSS 盒子模型 二
Sublime 快捷键: 文件保存后,输入 html:xt + tab ,补全html html:xt <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...
- jfinal如何设置使用哪种模板引擎(视图)
1.jfinal\com\jfinal\core\Controller.java /** * Render with view use default type Render configured i ...
- .net framework 类库中必须掌握的命名空间(或者类)
Web开发常用命名空间和类. System.Collections //命名空间包含接口和类,这些接口和类定义各种对象(如列表.队列.位数组.哈希表和字典)的集合.System.Collections ...
- MySQL double 类型查询不准确的问题
问题 有如下查询: SELECT * FROM <table-name> WHERE price > 32.99; 结果竟然包含了 32.99 的数据行. 原因 price 的类型是 ...
- 【紫书】【重要】Not so Mobile UVA - 839 递归得漂亮
题意:判断某个天平是否平衡,输入以递归方式给出. 题解:递归着输入,顺便将当前质量作为 &参数 维护一下,顺便再把是否平衡作为返回值传回去. 坑:最后一行不能多回车 附:天秀代码 #defin ...