Codeforces Round #550 (Div. 3) A Diverse Strings

A. Diverse Strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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".

Input

The first line contains integer nn (1n1001≤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.

Output

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.

Example
Input
8
fced
xyz
r
dabcef
az
aa
bad
babc
Output
 
Yes
Yes
Yes
Yes
No
No
No
No

解题思路:这道题就是给你一串字符串,判断其是否符合题意,如果它是连续且不出现重复则符合,输出Yes,否则输出No;
解法一:直接对字符串排序;
代码如下:
 1 #include<iostream>
 2 #include<map>
 3 #include<algorithm>
 4 #include<string.h>
 5 using namespace std;
 6 
 7 int n ;
 8 string s ;
 9 int flag1 = 1;
10 int flag2 = 1;
11 int a[1000];
12 int count1 = 0;
13 map<char,int>mp;            //用于记录是否有重复的字母;
14 int main()
15 {
16     cin>>n;
17     while(n--)
18     {
19         20         flag1 = 1;       //flag1是用于标记是否有重复的,现在假设没有重复的,置flag=1;
21         flag2 = 1;       //flag2是用于记录是否连续;
22         cin>>s;
23         sort(s.begin(),s.end());     //将字符串排序;
24     
25         for(int i = 0 ; i < s.size();i++)
26         {
27             mp[s[i]]++;      //看是否有重复的;先记录下每个字母的个数;
28         }
29         for(int i = 'A'-'0';i <= 'Z'-'0';i++)
30         { 
31             if(mp[i]>1)          //字母有重复;
32             {
33                 flag1 = 0;       //将flag1 置为0;
34                 mp[i] = 0;        
35             }
36 
37         }
38         for(int i = 1 ; i < s.size() ;i++)
39         {
40             if((s[i]-'0')!=(s[i-1]-'0')+1)      //判断是否连续;如果不连续就是相邻的Ascall码相差不为1;
41             {
42                 flag2 = 0;                 //不连续则将flag2 置为0;
43             }
44         }
45       if(flag1==0||flag2==0)          //如果字母重复或者字母不连续;
46       {
47           cout<<"No"<<endl;
48       }else 
49       cout<<"Yes"<<endl;
50     }
51     return 0;
52 }

 解法二:不利用头文件带的函数,将字符串转为数字再进行数字的排序再存进另一个字符串;这个是刚刚比赛的时候写的,忘记了字符串可以直接排序,所以在思考字符串怎么排序;

就想到可以先转为数字排序再放进另一个字符串;

代码如下:

 1 #include<iostream>
 2 #include<map>
 3 #include<algorithm>
 4 #include<string.h>
 5 using namespace std;
 6 
 7 int n ;
 8 string s ;
 9 map<char,int>mp;
10 int flag1 = 1;
11 int flag2 = 1;
12 int a[1000];
13 int count1 = 0;
14 string t;
15 int main()
16 {
17     cin>>n;
18     while(n--)
19     {
20         count1 = 0;
21         flag1 = 1;          //用于标记是否重复;
22         flag2 = 1;          //用于标记是否连续;
23         cin>>s;
24         for(int i = 0 ; i < s.size();i++)
25         {
26             a[i] = s[i]-'0';       //将字符串每位转为数字;
27             count1++;              //并计算该数字数组的长度;
28         } 
29         sort(a,a+count1);            //对数字进行排序;
30         t = "";
31         for(int i = 0 ; i < count1 ;i++)
32         {
33             t.push_back(a[i]+'0');          //将数字转为字符放入另一个字符串;
34         }
35         for(int i = 0 ; i < s.size();i++)
36         {
37             mp[s[i]]++;                   //记录每个字母出现的个数;
38         }
39         for(int i = 'A'-'0';i <= 'Z'-'0';i++)
40         {
41             if(mp[i]>1)              //如果出现重复;
42             {
43                 flag1 = 0;              //将flag1 置为0 ;
44                 mp[i] = 0;
45             }
46 
47         }
48         for(int i = 1 ; i < t.size() ;i++)
49         {
50             if((t[i]-'0')!=(t[i-1]-'0')+1)            //如果不连续;就是相邻的Ascall码相差不为1;
51             { 
52                 flag2 = 0;                     //将flag2 置为0;
53             }
54         }
55       if(flag1==0||flag2==0)       //如果有重复或者不连续;
56       {
57           cout<<"No"<<endl;
58       }else 
59       cout<<"Yes"<<endl;
60     }
61     return 0;
62 }

猜你喜欢

转载自www.cnblogs.com/yewanting/p/10634188.html