CodeForces 709C Letters Cyclic Shift

本文介绍了一种通过选择字符串中的非空子串并将其字母循环向前移一位来获得字典序最小字符串的方法。具体实现中,算法遍历输入字符串以找到最佳移位位置,并提供了一个C++示例程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Letters Cyclic Shift
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z 'y 'x 'b 'a 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'.

What is the lexicographically minimum string that can be obtained from s by performing this shift exactly once?

Input

The only line of the input contains the string s (1 ≤ |s| ≤ 100 000) consisting of lowercase English letters.

Output

Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.

Examples
input
codeforces
output
bncdenqbdr
input
abacaba
output

aaacaba

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <string>

using namespace std;
const int maxn=1e5;
char a[maxn+5];
char fun(char x)
{
    char b=x;
    if(x=='a')
        b='z';
    else
        b--;
    return b;
}
int main()
{
    scanf("%s",a);
    int len=strlen(a);
    int l=len-1,r=len-1;
    for(int i=0;i<len;i++)
    {
        if(l==len-1&&a[i]!='a')
            l=i;
        if(l!=len-1&&r==len-1&&a[i]=='a')
            r=i-1;
    }
    for(int i=0;i<len;i++)
    {
        if(i<=r&&i>=l)
            printf("%c",fun(a[i]));
        else
            printf("%c",a[i]);

    }
    printf("\n");
    return 0;
}



转载于:https://www.cnblogs.com/dacc123/p/8228588.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值