CodeForces 709C Letters Cyclic Shift

本文介绍了一种算法,该算法接收一个由小写字母组成的非空字符串,并从中选择一个非空子串进行字母循环左移操作,即将每个字符替换为英文字母表中的前一个字母,特别地将'a'替换为'z'。目标是通过仅执行一次这样的移位操作来获得字典序最小的可能字符串。

摘要生成于 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值