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为多少
【类型】
implementation
【分析】

首先,何为字典序最小,大家应该都理解

然后,题目的替换操作,很明显会将字符串s的字典序变小,但是唯一一个特例是字母'a',它替换之后反而会使得字典序变小

于是乎,字母'a'成了突破口,凡是遇到字母'a',能不替换就不替换

也就意味着,我们要替换除'a'之外的其他字母


上述这种,能够替换的有两部分,红色虚线及绿色虚线,从字典序大小考虑出发,越靠前的字母变小,整体字典序越小

所以,我们会替换红色虚线处的字母,而不是绿色虚线处的字母


当然,此题最值得注意的是"exactly one non-empty substring"

这就意味着全'a'串也要变,即字符串"aaaaaaa",我们要替换其中的字母(会使得字典序比原来大),但又要使字典序最小,所以只能将最后一个'a'->'z'

【时间复杂度&&优化】
O(n)

题目链接→Codeforces Problem 708A Letters Cyclic Shift

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int i,k=0;
 6     char s[100005];
 7     gets(s);
 8     int len=strlen(s);
 9     for(i=0;s[i]!='\0';i++)
10         if(s[i]!='a')
11             break;
12     for(;s[i]!='\0';i++)
13     {
14         if(s[i]=='a')
15             break;
16         s[i]--;
17         k++;
18     }
19     if(!k)
20         s[strlen(s)-1]='z';
21     puts(s);
22     return 0;
23 }
复制代码

 

posted @   Angel_Kitty  阅读(370)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示
剑桥
14:14发布
剑桥
14:14发布
6°
南风
3级
空气质量
相对湿度
87%
今天
多云
4°/16°
周日
9°/18°
周一
大雨
8°/15°