AIM Tech Round 3 (Div. 1) A. Letters Cyclic Shift 贪心
A. Letters Cyclic Shift
题目连接:
http://www.codeforces.com/contest/708/problem/A
Description
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.
Sample Input
codeforces
Sample Output
bncdenqbdr
Hint
题意
你可以调整一个子串,使得这个子串的所有字符都往前减小一个字典序,问你能够得到的最小字典序的字符串是哪个
题解:
贪心,从前往后,能变就变。
特判掉全是a的情况就好了
代码
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin>>s;
int i;
for(i=0;i<s.size();i++)
if(s[i]!='a')break;
int flag = 0;
for(int j=i;j<s.size();j++)
{
if(s[j]=='a')break;
s[j]--;
flag=1;
}
if(i==s.size())s[i-1]='z';
cout<<s<<endl;
}