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;
}
全部评论

相关推荐

今天 18:27
已编辑
门头沟学院 C++
26学院本太难了,很多公司机筛就给我刷了。机会都难拿到如果是简历存在问题也欢迎拷打————————————————————分割线——————————————————————2026.3.4更新:发完贴之后,时不时投递又收到了不少的笔试/面试邀请。主要是之前投递简历出去之后基本上都是沉默状态,年后好转了不少timeline:2026.01.21&nbsp;文远知行笔试,半年多没刷算法题&nbsp;-&gt;挂&nbsp;(后续HR说春招可以重新安排笔试)2026.2.4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;小鹏汇天&nbsp;技术一面,第二周收到结果&nbsp;-&gt;挂2026.2.12&nbsp;&nbsp;&nbsp;大众Cariad代招&nbsp;技术二面&nbsp;-&gt;Offer2026.2.28&nbsp;&nbsp;&nbsp;多益网络技术面试,由于风评太差,一直在犹豫要不要接面试&nbsp;-&gt;推迟-----------分割线-----------2026.3&nbsp;月前的某一天,临时去电网报名了二批计算机岗位的笔试2026.3.6&nbsp;从上家公司实习离职,氛围最好的一家公司,leader&nbsp;说可以帮忙转正,但是流程太长,而且我们部门据说只有一个&nbsp;hc,更想要研究生,我很有可能是会被签外包公司在这里干活,就离职了。2026.3.9&nbsp;入职新公司,大众Cariad&nbsp;以外部公司的身份进组,项目组签了三年,后续三年应该都可以在这里呆,不知道有没有希望原地跳槽。2026.3.10&nbsp;电网考试居然说我通过资格审查了,短信约我去参加资格审查,请假一天,买了&nbsp;12&nbsp;号晚上的机票回成都2026.3.15&nbsp;参加国家电网三新计算机类的笔试2026.3.17&nbsp;电网出成绩了,感觉很低。觉得已经🈚️了2026.3.18&nbsp;收到电网面试通知,通知&nbsp;3.22-3.25&nbsp;这个时间去面试,我的岗位只招&nbsp;1&nbsp;个人。据说面试只有&nbsp;2-3&nbsp;人,不知道能不能成功
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务