A. Vitaly and Strings

本文探讨了在给定两个相同长度的字符串s和t的情况下,如何找出一个满足特定条件的字符串:该字符串的字典序大于s且小于t,并且只包含小写字母。通过分析示例输入和输出,理解并实现了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.

During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.

Let's help Vitaly solve this easy problem!

Input

The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.

The second line contains string t (|t| = |s|), consisting of lowercase English letters.

It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.

Output

If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).

If such string exists, print it. If there are multiple valid strings, you may print any of them.

Sample test(s)
input
a
c
output
b
input
aaa
zzz
output
kkk
input
abcdefg
abcdefh
output
No such string
做了很长时间一直WA,主要是考虑不充分。先考虑末尾的情况,然后如果前面对应第i个字符中第二串比第一串大于等于2就可以直接使得s[i]=s1[i]+1,然后其他的用'z'补齐,如果前面对应第i个字符中第二串比第一串大1就要分两种情况讨论,第一种是看s1中剩下字符是不是都为'z',只要一个不是,s[i]=s1[i],i后面的都用'z'补齐并输出。如果这种情况不满足,那么就看s2中是不是所有的字符都是'a',如果有一个不是,s[i]=s2[i],i后面的都用'a'补齐并输出。
#include<stdio.h>
#include<string.h>
char s1[200],s2[200],s[200];
int main()
{
	int n,m,i,j,len,flag,flag1,flag2,flag3;
	while(scanf("%s%s",s1,s2)!=EOF)
	{
		len=strlen(s1);
		flag=1;
		flag1=0;
		memset(s,0,sizeof(s));
		for(i=0;i<len;i++){
			if(i==len-1 && s2[i]-s1[i]<=1){
				flag=0;break;
			}
			else if(i==len-1 && s2[i]-s1[i]>=2){
				s[i]=s1[i]+1;break;
			}
			else if(s2[i]-s1[i]>=2){
				s[i]=s1[i]+1;
				for(j=i+1;j<len;j++){
					s[j]='z';
				}
				break;
			}
			else if(s2[i]-s1[i]==1){
				flag2=0;
				for(j=i+1;j<len;j++){
					if(s1[j]!='z'){
						flag2=1;break;
					}
				}
				if(flag2==1){
					s[i]=s1[i];
					for(j=i+1;j<len;j++){
						s[j]='z';
					}
					break;
				}
				else if(flag2==0){
				      flag3=0; 
				      for(j=i+1;j<len;j++){
				         if(s2[j]!='a'){
      					flag3=1;break;
      				    }
				      }
      				 if(flag3==0){
 				      	flag=0;break;
 				      }
 				      else{
      				 	s[i]=s2[i];
      				 	for(j=i+1;j<len;j++){
	 				      	s[j]='a';
	 				      }
	                      break;
      				 }
				}
			}
			
			else if(s2[i]==s1[i]){
				s[i]=s1[i];continue;
			}
			else if(s1[i]>s2[i]){
				flag=0;break;
			}
			
		}
		if(flag==1)printf("%s\n",s);
		else printf("No such string\n");
	}
	return 0;
}
The following is the data that you can add to your input file (as an example). Notice that the first line is going to be a line representing your own hobbies. In my case, it is the Vitaly,table tennis,chess,hacking line. Your goal is to create a class called Student. Every Student will contain a name (String) and an ArrayList<String> storing hobbies. Then, you will add all those students from the file into an ArrayList<Student>, with each Student having a separate name and ArrayList of hobbies. Here is an example file containing students (the first line will always represent yourself). NOTE: eventually, we will have a different file containing all our real names and hobbies so that we could find out with how many people each of us share the same hobby. Vitaly,table tennis,chess,hacking Sean,cooking,guitar,rainbow six Nolan,gym,piano,reading,video games Jack,cooking,swimming,music Ray,piano,video games,volleyball Emily,crochet,drawing,gardening,tuba,violin Hudson,anime,video games,trumpet Matt,piano,Reading,video games,traveling Alex,swimming,video games,saxophone Roman,piano,dancing,art Teddy,chess,lifting,swimming Sarah,baking,reading,singing,theatre Maya,violin,knitting,reading,billiards Amy,art,gaming,guitar,table tennis Daniel,video games,tennis,soccer,biking,trumpet Derek,cooking,flute,gaming,swimming,table tennis Daisey,video games,guitar,cleaning,drawing,animated shows,reading,shopping Lily,flute,ocarina,video games,baking Stella,roller skating,sudoku,watching baseball,harp Sophie,viola,ukulele,piano,video games
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值