codeforces 518 A. Vitaly and Strings

codeforces 518 A

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 strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let’s help Vitaly solve this easy problem!

题目大意:

寻找一个字典序大于s且小于t的字符串( s < t )

数据范围:

|s| = |t| <= 100 (这么明显用暴力即可)

解题思路:

从后往前遍历s寻找第一个不为’z’的s[i]。
如果s[i] == ‘z’,就令s[i] = ‘a’;如果s[i] != ‘z’,就将该字符ASCII加一并退出循环,此时s一定大于原s,如果s != t就输出s,否则不存在

主要是有特别情况值得记录,比如:

输入:pnzcl
pnzdf
输出:pnzcm

本题代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;

string s, t;

int main() {
    cin >> s >> t;
    int len = s.size();
    bool  flag = 0;
    for(int i = len - 1; i >= 0; i--) {
        if(s[i] != 'z') {
            s[i]++;
            break;
        }
        if(s[i] == 'z') s[i] = 'a';
    }
    if(s != t) cout << s << endl;
    else cout << "No such string" << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值