CodeForces - 518A Vitaly and Strings(水题)

来源:互联网 发布:淘宝xbox360哪家店好 编辑:程序博客网 时间:2024/05/16 04:57

题意:

输入两个字符串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;}
0 0
原创粉丝点击