题意:
输入两个字符串s和t(只由小写字符组成),两个字符串的长度相同,s的字典序 < t的字典序。求一个字符串其字典序介于s和t之间。
解析:
显然求s的字典序+1最好,但是要保证最后是z的时候,要将当前位变为a,并且将下一位+1。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 105;
char s[N], t[N], ans[N];
int main() {
while(scanf("%s%s", s, t) != EOF) {
int len = strlen(s);
strcpy(ans, s);
for(int i = len-1; i >= 0; i--) {
if(ans[i] == 'z') {
ans[i] = 'a';
}else {
ans[i]++;
break;
}
}
if(strcmp(ans, t) >= 0) puts("No such string");
else puts(ans);
}
return 0;
}
本文介绍了一种算法,用于生成一个特定的字符串,该字符串的字典序位于两个给定字符串s和t之间。通过逐字符地增加s的值来找到最小的可能字符串,同时确保该字符串不超过t的字典序。
742

被折叠的 条评论
为什么被折叠?



