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?
The only line of the input contains the string s (1 ≤ |s| ≤ 100 000) consisting of lowercase English letters.
Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.
codeforces
bncdenqbdr
abacaba
aaacaba
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.
#include<bits/stdc++.h>
using namespace std;
int main() {
string str;
int i,j;
cin>>str;
for(i=0;i<str.size();i++){
if(str[i]!='a'){
break;
}
}
for(j=i;j<str.size();j++){
if(str[j]=='a'){
break;
}
str[j]--;
}
if(i==str.size()){
str[str.size()-1]='z';
}
cout<<str<<endl;
return 0;
}
本文介绍了一种算法挑战,任务是在给定的字符串中选择一个非空子串,并将其中的所有字母按照英文字母表向前移动一位。目标是找到执行此操作后得到的字典序最小的字符串。
419

被折叠的 条评论
为什么被折叠?



