问题描述
题目链接:http://codeforces.com/problemset/problem/708/A
输入字符串, 用至少一个长度的子串,按照一定规则将原字符串替换,使得变化后的字符串的字典序最小。
给出一个样例:
input
aaaa
output
aaaz
即最少替换一个,即使它的字典序可能不是最小的。
思路
由于a在字典序中最小,所以当判断为a时不进行替换,其余的字典序减一,当字符串全为a时,将最后一个字符替换,最后输出结果。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
using namespace std;
const int N=1e5+5;
char s[N];
int main()
{
int i,len,flag=0;
gets(s);
len=strlen(s);
for(i=0;i<len;++i)
{
if(s[i]!='a')
{
flag=1;
--s[i];
}
else if(flag)
break;
}
if(!flag)
s[i-1]='z';
puts(s);
return 0;
}