一道水题,不过错了很多次,注意一下思维。
Description
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 stringss and t to Vitaly. The strings have the same length, they consist of lowercase English letters, strings is lexicographically smaller than stringt. Vitaly wondered if there is such string that is lexicographically larger than strings and at the same is lexicographically smaller than stringt. This string should also consist of lowercase English letters and have the length equal to the lengths of stringss and t.
Let's help Vitaly solve this easy problem!
Input
The first line contains strings (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here,|s| denotes the length of the string.
The second line contains stringt (|t| = |s|), consisting of lowercase English letters.
It is guaranteed that the lengths of stringss and t are the same and strings 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 Input
a
c
b
aaa
zzz
kkk
abcdefg
abcdefh
No such string
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char s1[110], s2[110];
int main()
{
int flag = 0;
while(~scanf("%s%s", s1, s2)){
int len = strlen(s1);
for(int i = len-1; i >= 0; --i){
if(s1[i] != 'z'){
s1[i]++; flag = 1;
break;
}
else
s1[i] = 'a';
}
if(strcmp(s1, s2) >= 0 || flag == 0)
printf("No such string\n");
else
printf("%s\n", s1);
}
return 0;
}
参考博客:http://www.cnblogs.com/I-love-HLD/p/4299364.html