题意:给你两个长度相同的字符串,输出一个长度与它俩相同,且字典序位于两者之间(不可与其中一者相同)的字符串,若不存在则输出“No such string”。(输入保证字典序第一个严格小于第二个,且长度相同)
水题,让最后一个加一个单位的字典序然后检查是否符合条件即可(注意为‘z‘的时候要进位,若长度超过则也不符合)
#include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<deque> #include<queue> #include<stack> #include<list> typedef long long ll; typedef unsigned long long llu; const int MAXN = 100 + 10; const int MAXT = 10000 + 10; const int INF = 0x7f7f7f7f; const double pi = acos(-1.0); const double EPS = 1e-6; using namespace std; char s1[MAXT], s2[MAXT], s[MAXT]; int main(){ scanf("%s%s", s1, s2); int len = strlen(s1); int lur = len - 1; bool flag = true; if(s1[lur] == ‘z‘){ while(lur >= 0 && s1[lur] == ‘z‘) s1[lur] = ‘a‘, --lur; if(lur == -1) flag = false; else s1[lur] = s1[lur] + 1; } else s1[lur] = s1[lur] + 1; if(strcmp(s1, s2) >= 0) flag = false; if(!flag) printf("No such string\n"); else printf("%s\n", s1); return 0; }
时间: 2024-10-16 07:40:14