codeforces 518A. Vitaly and Strings

本文介绍了一个关于寻找特定字符串的问题:给定两个字符串s和t,找出一个字典序介于两者之间的字符串。文章提供了完整的C++代码实现,通过递增最后一个非'z'字符来解决这个问题。

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

A. Vitaly and Strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Note

String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti.

大意是找一个序列长度和s,t的长度相同,并且字典序在s之后,在t之前。

看了数据之后发现有点小坑。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iomanip>
 5 #include<cctype>
 6 #include<string>
 7 #include<cmath>
 8 #include<cstdio>
 9 #include<cstdlib>
10 #define LL long long
11 #define PF(x) ((x)*(x))
12 #define LF(x) ((x)*PF(x))
13 
14 using namespace std;
15 const int INF=1<<31-1;
16 const int max9=1e9;
17 const int max6=1e6;
18 const int max3=1e3;
19 
20 int gcd(int a,int b)
21 {
22     return b==0?a:gcd(b,a%b);
23 }
24 
25 int main()
26 {
27     char str1[120],str2[120];
28     while(cin >> str1 >> str2)
29     {
30         int len=strlen(str1);
31         int flag=0;
32         int x=len;
33         for(int i=len-1;i>=0;i--)
34         {
35             if(str1[i]!='z')
36             {
37                 flag=1;
38                 str1[i]++;
39                 break;
40             }
41             else str1[i]='a';
42         }
43         if(flag==0||strcmp(str2,str1)<=0) cout << "No such string" << endl;
44         else cout << str1 << endl;
45     }
46     return 0;
47 }
View Code

 

转载于:https://www.cnblogs.com/I-love-HLD/p/4299364.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值