AIM Tech Round 3 (Div. 2) C. Letters Cyclic Shift 贪心、字典序

来源:互联网 发布:python netsnmp安装 编辑:程序博客网 时间:2024/06/16 02:46

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 sand 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 thats1 = t1, s2 = t2, ..., si - 1 = ti - 1, and si < ti.


Source

AIM Tech Round 3 (Div. 2)


My Solution

贪心、字典序

必须改一个子串使得得到的新串字典序最小

所以从左往右改第一个不是a的, 然后连着的都要改直到碰到一个a为止

但是exactly one non-empty substring 

所以全是a的时候也要改, 把最后一个a改成 z


#include <iostream>#include <cstdio>#include <string>using namespace std;typedef long long LL;const int maxn = 1e6 + 8;string s;int main(){    #ifdef LOCAL    freopen("c.txt", "r", stdin);    //freopen("o.txt", "w", stdout);    int T = 3;    while(T--){    #endif // LOCAL    ios::sync_with_stdio(false);    cin.tie(0);    cin>>s;    int sz = s.size();    bool flag = false;    char ch;    for(int i = 0; i < sz; i++){        if(!flag){            if(s[i] == 'a') ;            else{                flag = true;                ch = s[i];                s[i] = (ch - 1);            }        }        else{            if(s[i] == 'a') break;            else{                ch = s[i];                s[i] = (ch - 1);            }        }    }    if(!flag){        s[sz - 1] = 'z';    }    cout<<s<<endl;    #ifdef LOCAL    cout<<endl;    }    #endif // LOCAL    return 0;}

  Thank you!

                                                                                                                                               ------from ProLights

0 0
原创粉丝点击