AIM Tech Round 3 (Div. 2) C.Letters Cyclic Shift

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


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=100010;
char s[N];

int main(){
	scanf("%s",s);
	int cnt=0;
	bool flag=0;
	bool flag2=0;
	int l=strlen(s);
	
	for(int i=0;i<l;i++){
		if(s[i]=='a'){
			cnt++;
			if(flag2){
				break;
			}
			if(flag){
				flag=0;
				continue;
			}
			flag=1;
				continue;
		}
		if(!flag||cnt==1){
			s[i]--;
			flag2=1;
		}
		if(cnt==2&&flag){
			break;
		}
		
	}

	if(cnt==l){
		s[l-1]='z';
	}

	printf("%s\n",s);
}

题意:给一串序列,把某一串不为空的子序列按照 'z 'y 'x 'b 'a 'z'变换后,求得到的最小字典序序列;

找两个非连续‘a'之间的子序列替换即可;没有‘a’则全部替换;全为‘a’则将最后替换成‘z’;

开始的想法:

第一个判断条件:遇到一个‘a’就把flag=1,如果之前已经flag=1,那么就把flag=0(即现在已经有两个连续的‘a’了),如果遇到‘a’时已经替换完成,则不用继续,直接break;并记录‘a’的个数;

第二个判断条件:如果flag=0,就替换(说明之前没有‘a’或者‘a’有2个连续‘a’);或者如果有一个‘a’,然后之后有一段序列就替换;这一步操作后flag2=1;

第三个判断条件:如果已经出现2个‘a’并且已经执行替换操作(flag2=1)就break掉;

最后考虑特殊情况,如果全是‘a’就把最后一个‘a’替换成‘z’;

(这是没过main test 的代码,之后应该会修改:)但思路应该没有什么变化~)

========================

main test 成功WA~

好像是只考虑两个连续没考虑三个??待我看看WA的数据……

========================

没看数据直接试aaabc结果错。。

所以想法应该是连续的‘a’不考虑;

判断第一个字符是否为‘a’,为则一直替换到‘a’;否则找到一个非‘a’,替换之后直到‘a’的字符;

果然思路简化后A了;之前看着例子去考虑反而想复杂了(泪);

修改版代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=100010;
char s[N];

int main(){
	scanf("%s",s);
	int cnt=0;
	bool flag2=0;
	int l=strlen(s);
	for(int i=0;i<l;i++){
		if(s[i]=='a'){
			if(flag2) break;
			cnt++;
		}
		else{
			s[i]--;
			flag2=1;
		}
	}
	if(cnt==l) s[l-1]='z';

	printf("%s\n",s);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值