Chewbaсca and Number CF 518A(水题)

本文针对一个特定的字符串比较问题,提出了一种简单的解决方案。通过将字符串视为数字,并使用类似整数加法的方法来实现字符串之间的比较,从而找到满足条件的中间字符串。

题目链接:点击打开链接

题意分析:给定两行长度相同的字符串s t ,求是否有以长度相同的字符串m使得字典序 s < m < t,若有输入任意一组答案即可。

                    将两行字符串看做两个数字s,t 即可,问题就转换为是否有一整数m满足s < m  < t,不存在的情况只有t - s == 1 时。

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<string>
#include <algorithm>
using namespace std;
const int maxn = 105;
string s1,s2;
int main()
{
    cin >> s1 >> s2;
    int len = s1.length();
    s1[len - 1]++;
    int cnt = 0 ;
    if(s1[len - 1] > 'z')//与整数加1进位一样
    {
        s1[len - 1] = 'a';
        cnt = 1;
    }
    for(int i = len - 2; i >= 0; i--)
    {
        if(cnt == 1)
        {
            s1[i]++;
            if(s1[i] > 'z')
            {
                s1[i] = 'a';
            }
            else cnt = 0;
        }
    }
    if(s1 == s2) printf("No such string\n");
    else cout << s1 << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值