(原创)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 ...
随机推荐
- POJ2236(并查集入门)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 22977 Accepted: 961 ...
- Java基础--CountDownLatch
CountDownLatch是线程同步辅助类,它允许一个或多个线程wait直到countdown被调用使count为0. CountDownLatch是在java1.5被引入,存在于java.util ...
- (转)在Windows平台上安装Node.js及NPM模块管理
本文转载自:http://www.cnblogs.com/seanlv/archive/2011/11/22/2258716.html 之前9月份的时候我写了一篇关于如何在Windows平台上手工管理 ...
- (转)ashx 使用Session
本文转载自:http://www.cnblogs.com/TivonStone/archive/2012/04/06/2434796.html 最近做一个项目,调用ashx文件,其中ashx文件里面有 ...
- 关于C#客户端引用C++ dll的问题
近期在做项目的过程中需要在Winform客户端项目引用由C++编译的DLL,于是对相关的内容进行了一些研究,有几点心得总结如下. 第一步是制作要引用的类库: (1)首先拿到C++的dll,需要注意的是 ...
- TCG卡牌游戏研究:《炉石战记:魔兽英雄传》所做的改变
转自:http://www.gameres.com/665306.html TCG演进史 说到卡牌游戏,大家会联想到什么呢? 是历史悠久的扑克牌.风靡全球的<MTG 魔法风云会>与< ...
- ffmpeg: ‘UINT64_C’ was not declared in this scope (转)
ffmpeg 默认是用C文件来编译的,如果某个CPP文件想引用ffmpeg中的某些函数或者头文件,有可能出现 ‘UINT64_C’ was not declared in this scope的错误 ...
- Synchronized关键字、Lock,并解释它们之间的区别
Synchronized 与Lock都是可重入锁,同一个线程再次进入同步代码的时候.可以使用自己已经获取到的锁. Synchronized是悲观锁机制,独占锁.而Locks.ReentrantLock ...
- 在异步回调中调用MessageBox.Show
public static void Test() { ThreadStart aThreadStart = delegate() { ); MessageBox.Show("Good!&q ...
- Codeforces 1137C Museums Tour (强连通分量, DP)
题意和思路看这篇博客就行了:https://www.cnblogs.com/cjyyb/p/10507937.html 有个问题需要注意:对于每个scc,只需要考虑进入这个scc的时间即可,其实和从哪 ...