A. Diverse Strings t

cf:http://codeforces.com/contest/1144/problem/A

A. Diverse Strings
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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”.

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
inputCopy
8
fced
xyz
r
dabcef
az
aa
bad
babc
outputCopy
Yes
Yes
Yes
Yes
No
No
No
No

水题:简单理解题:
翻译:
如果字符串包含拉丁字母的连续(相邻)字母,并且每个字母恰好出现一次,则称为字符串。例如,以下字符串是多种多样的:“fced”,“xyz”,“r”和“dabcef”。以下字符串不是多种多样的:“az”,“aa”,“bad”和“babc”。请注意,字母“a”和“z”不相邻。
形式上,考虑字母表中字符串中所有字母的位置。这些位置应该形成连续的部分,即它们应该一个接一个地没有任何间隙。
您将获得一系列字符串。对于每个字符串,如果它不同,请打印“是”。否则,请打印“否”。
题意:输入一个数字,代表有多少组测试数据,每组数据一个字符串,这个字符串必须满足一下条件,一个必须是每个字符出现一个的,而且能按字母表排序abc这种相邻的排序方式就输出Yes,否则输出No;
解题思路:就按照题意,用sort排一下序,比较一下后一个与前一个之间是否相邻;

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{
    int t;
    char a[150];
    scanf("%d",&t);
    while(t--)
    {
        int i;
        memset(a,0,sizeof(a));
        cin>>a;
        int s=strlen(a);
        sort(a,a+s);
        int k=0;
        for(i=0;i<s;i++)
        {
            if(a[i+1]-a[i]==1&&i+1<s){
                    k++;
            }
        }
        if(k==s-1)
        {
            printf("Yes\n");
        }
        else
            printf("No\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43516113/article/details/88938596