题目大意:第一个字符串小于第二个字符串。现在要找到一个字符串大于第一个字符串,小于第二个字符串。
思路:其实这边不需要遍历,将两个字符串看成数来处理就可以了。
这样的话就等价成两个数当中是否相邻的问题。
所以只需要对第一个字符串最后一个字符加1,这样就能够保证大于第一个字符串,而且是离第一个字符串最近的那一个。
加1之后可能要进行进位处理。
然后与第二个字符串比较一下就可以了。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str1[110], str2[110];
int main()
{
while (~scanf("%s%s", str2, str1))
{
int len = strlen(str2);
int cnt = 0;
for (int i = len - 1; i >= 0; i--)
{
str2[i] += 1;
cnt = (str2[i]-1) / ('z');
if (str2[i] > 'z')
str2[i] = 'a';
if (cnt == 0)
break;
}
if (strcmp(str2,str1)<0)
printf("%s\n", str2);
else
puts("No such string");
}
}