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.ExamplesinputCopy
codeforces
outputCopy
bncdenqbdr
inputCopy
abacaba
outputCopy
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 s 1 = t 1, s 2 = t 2, …, s i - 1 = t i - 1, and s i < t i.
题意:选一个子串替换子串内所有字符使字典序最小。
思路:由于字典序从前往后比较,那么从前往后找a,如果这个串都是a,就改最后一个。不然的话就从第一个a到出现第二个a的区间里把区间内的全部替换。这样是最小的。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
char s[30]={"zabcdefghijklmnopqrstuvwxy"};
LL num[maxn];
int main(void)
{
string str;cin>>str;
LL sum=0;
for(LL i=0;i<str.size();i++)
{
if(str[i]=='a') sum++;
}
if(sum==str.size())
{
for(LL i=0;i<str.size()-1;i++) cout<<str[i];
cout<<"z"<<endl;
return 0;
}
bool flag=1;
for(LL i=0;i<str.size();i++)
{
if(str[i]=='a'&&flag==1) cout<<str[i];
else if(str[i]=='a'&&flag==0)
{
for(LL j=i;j<str.size();j++)
cout<<str[j];
break;
}
else if(str[i]!='a')
{
flag=0;
cout<<s[str[i]-'a'];
}
}
return 0;
}