AIM Tech Round 3 (Div. 2) C. Letters Cyclic Shift (字符串)

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

Note

String s is lexicographically smaller than some other string t of the same length if there exists some 1 ≤ i ≤ |s|, such that s1 = t1, s2 = t2, …, si - 1 = ti - 1, and si < ti.

题意:给你一个字符串,现在有一种变化,就是将一个字符变为它前面的字符, a 前面是z,求必须改变一个子串后的最小字典序字符串。

思路:因为要使得字典序最小,所以说,我们选择的子串中的字符变化后必定都是要比原来的小的,所以说,我们选择第一个满足条件的子串进行变化即可,因为必须要变化,所以说,如果字符串中不存在满足条件的子串,那我们只能将最后一个字符变化。

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-25-16.05 Thursday
File Name    : D:\MyCode\2016-8月\2016-8-25.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define LL long long
#define ll __int64
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define gn (sqrt(5.0)+1)/2
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
const ll INF=1e9+10;
const int MAXN=1e6+10;
const int MOD=1e9+7;
//head
char str[MAXN];
char change(char ch)
{
    if(ch>'a'&&ch<='z')
        return ch-1;
    else
        return 'z';
}
int main()
{
    scanf("%s",str);
    int len=strlen(str);
    int l=-1,r=-1;
    for(int i=0;i<len;i++)
    {
        char ch=change(str[i]);
        if(ch<str[i])
        {
            l=i;break;
        }
    }
    if(l==-1)
    {
        str[len-1]=change(str[len-1]);
        printf("%s",str);
        return 0;
    }
    for(int i=l+1;i<len;i++)
    {
        char ch=change(str[i]);
        if(ch>str[i])
        {
            r=i-1;break;
        }
    }
    if(r==-1)
    {
        for(int i=l;i<len;i++)
            str[i]=change(str[i]);
    }
    else
    {
        for(int i=l;i<=r;i++)
            str[i]=change(str[i]);
    }
    printf("%s",str);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值