(原创)Codeforces Round #550 (Div. 3) A Diverse Strings
1 second
256 megabytes
standard input
standard output
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.
You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".
The first line contains integer nn (1≤n≤1001≤n≤100), denoting the number of strings to process. The following nn lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between 11 and 100100, inclusive.
Print nn 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.
8
fced
xyz
r
dabcef
az
aa
bad
babc
Yes
Yes
Yes
Yes
No
No
No
No 解题思路:这道题就是给你一串字符串,判断其是否符合题意,如果它是连续且不出现重复则符合,输出Yes,否则输出No;
解法一:直接对字符串排序;
代码如下:
#include<iostream>
#include<map>
#include<algorithm>
#include<string.h>
using namespace std; int n ;
string s ;
int flag1 = ;
int flag2 = ;
int a[];
int count1 = ;
map<char,int>mp; //用于记录是否有重复的字母;
int main()
{
cin>>n;
while(n--)
{
flag1 = ; //flag1是用于标记是否有重复的,现在假设没有重复的,置flag=1;
flag2 = ; //flag2是用于记录是否连续;
cin>>s;
sort(s.begin(),s.end()); //将字符串排序; for(int i = ; i < s.size();i++)
{
mp[s[i]]++; //看是否有重复的;先记录下每个字母的个数;
}
for(int i = 'A'-'';i <= 'Z'-'';i++)
{
if(mp[i]>) //字母有重复;
{
flag1 = ; //将flag1 置为0;
mp[i] = ;
} }
for(int i = ; i < s.size() ;i++)
{
if((s[i]-'')!=(s[i-]-'')+) //判断是否连续;如果不连续就是相邻的Ascall码相差不为1;
{
flag2 = ; //不连续则将flag2 置为0;
}
}
if(flag1==||flag2==) //如果字母重复或者字母不连续;
{
cout<<"No"<<endl;
}else
cout<<"Yes"<<endl;
}
return ;
}
解法二:不利用头文件带的函数,将字符串转为数字再进行数字的排序再存进另一个字符串;这个是刚刚比赛的时候写的,忘记了字符串可以直接排序,所以在思考字符串怎么排序;
就想到可以先转为数字排序再放进另一个字符串;
代码如下:
#include<iostream>
#include<map>
#include<algorithm>
#include<string.h>
using namespace std; int n ;
string s ;
map<char,int>mp;
int flag1 = ;
int flag2 = ;
int a[];
int count1 = ;
string t;
int main()
{
cin>>n;
while(n--)
{
count1 = ;
flag1 = ; //用于标记是否重复;
flag2 = ; //用于标记是否连续;
cin>>s;
for(int i = ; i < s.size();i++)
{
a[i] = s[i]-''; //将字符串每位转为数字;
count1++; //并计算该数字数组的长度;
}
sort(a,a+count1); //对数字进行排序;
t = "";
for(int i = ; i < count1 ;i++)
{
t.push_back(a[i]+''); //将数字转为字符放入另一个字符串;
}
for(int i = ; i < s.size();i++)
{
mp[s[i]]++; //记录每个字母出现的个数;
}
for(int i = 'A'-'';i <= 'Z'-'';i++)
{
if(mp[i]>) //如果出现重复;
{
flag1 = ; //将flag1 置为0 ;
mp[i] = ;
} }
for(int i = ; i < t.size() ;i++)
{
if((t[i]-'')!=(t[i-]-'')+) //如果不连续;就是相邻的Ascall码相差不为1;
{
flag2 = ; //将flag2 置为0;
}
}
if(flag1==||flag2==) //如果有重复或者不连续;
{
cout<<"No"<<endl;
}else
cout<<"Yes"<<endl;
}
return ;
}
(原创)Codeforces Round #550 (Div. 3) A Diverse Strings的更多相关文章
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round #486 (Div. 3) A. Diverse Team
Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Des ...
- Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造
Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 ht ...
- 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation
题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...
- CodeForces Round #550 Div.3
http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...
- (原创)Codeforces Round #550 (Div. 3) D. Equalize Them All
D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #275(Div. 2)-C. Diverse Permutation
http://codeforces.com/contest/483/problem/C C. Diverse Permutation time limit per test 1 second memo ...
- Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths
F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 ...
- D. Equalize Them All Codeforces Round #550 (Div. 3)
D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- OracleParameter中参数名必须是DB中已有字段:【 ORA-01745: 无效的主机/绑定变量名】
错误例子: 错误原因:查询的变量是自己随便命名的,不是数据库表中已有的字段, 因此,在进行OracleParameter(":rownum",10)时,在数据库中无法自动进行字段匹 ...
- LINUX必须记住的指令
写在前面: 1,<你一定要知道的关于Linux文件目录操作的12个常用命令>是楼主收集的关于Linux文件目录操作最常用的命令,包括文件或目录的新建.拷贝.移动.删除.查看等,是开发人员操 ...
- Network(lca暴力)
Network Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) Total Submi ...
- js生成邀请码(2)
//生成邀请码方法一 /*function createInviteCode() { var s = [],a=6,b=10; var chars = "123456789QWERTYUIP ...
- 关于python+django操作数据库中的表
数据库中的表示这样设计的 class C(models.Model): name = models.CharField(max_length=32) class B(models.Model): na ...
- 11-18网页基础--第二部分CSS样式属性(1)
第一课:样式属性 style 样式:style样式不仅可以直接在<body>中设置成整个网页的样式.格式:为了将样式.格式多样化,也可以将style单独抽出来,作为一个独立的个体,放在&l ...
- C语言学习笔记--指针阅读技巧
1. 指针阅读技巧:右左法则 (1)从最里层的圆括号中未定义的标示符看起 (2)首先往右看,再往左看 (3)遇到圆括号或方括号时可以确定部分类型,并调转方向 (4)重复 2.3 步骤,直到阅读结束 注 ...
- 使用NDK编译 libyuv <转>
官方源码:http://code.google.com/p/libyuv/简介: libyuv is an open source project that includes YUV scaling ...
- C++面向对象类的实例题目五
题目描述: 编写一个程序,采用一个类求n!,并输出5!的值. 程序代码: #include<iostream> using namespace std; class CFactorial ...
- java web前端发送请求的4种方式
表单 action, 链接href,js 绑定按钮 ajax绑定标签 <h1>通过表单提交参数</h1> <form action="/day46_v1/Ser ...