Codeforces 708A Letters Cyclic Shift

 
A. 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.

【题意】

给你一个由小写英文字母组成的非空字符串s,从中挑选出一个非空的子串,

将该子串中的每个字母均替换成前一个字母,如 'b' 换成 'a','c' 换成 'b',以此类推,特别的,'a' 要换成 'z'。

通过精确地对该字串进行一次移位,问可以从s中得到的字典序最小的字符串s为多少?

 

【分析】

  字典序: 对于字符串,先按首字符排序,如果首字符相同,再按第二个字符排序,以此类推。

  根据题意,字母换成前一个会使字典序变小(但是a换成z,会使字典序增大)

  因此就是要从给出的字符串中选出不含有a的字串,对其进行移位。

  而且,字串越靠前,移位改变的字典序越小。

  例如:codeforces

  这是不含有a的,本身就是一个符合条件的字串,对其进行移位得到:bncdenqbdr

  又例如:abacaba

  这个可以划分为3个字串:“b”、“c”、“b”

  对靠前的那个字串进行移位,得到:aaacaba

 

【时间复杂度】 O(n)


 

【代码实现】

 1 #include<stdio.h>
 2 int main(){
 3     int i,k;
 4     char s[10000];
 5     gets(s);
 6     int len = strlen(s);
 7     for(i=0; s[i]!='\0';i++){
 8         if(s[i]!='a')
 9             break;
10     }
11     for(;s[i]!='\0';i++){
12         if(s[i]=='a')
13             break;
14         s[i]--;
15         k++;
16     }
17     if(!k)
18         s[len-1]='z';
19     puts(s);
20     return 0;
21 } 

猜你喜欢

转载自www.cnblogs.com/fangxiaoqi/p/10193478.html