Codeforces 708A Letters Cyclic Shift
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?
The only line of the input contains the string s (1 ≤ |s| ≤ 100 000) consisting of lowercase English letters.
Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.
codeforces
bncdenqbdr
abacaba
aaacaba
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i,k=;
char s[];
gets(s);
int len=strlen(s);
for(i=;s[i]!='\0';i++)
if(s[i]!='a')
break;
for(;s[i]!='\0';i++)
{
if(s[i]=='a')
break;
s[i]--;
k++;
}
if(!k)
s[strlen(s)-]='z';
puts(s);
return ;
}
Codeforces 708A Letters Cyclic Shift的更多相关文章
- Codeforces Problem 708A Letters Cyclic Shift
题目链接: http://codeforces.com/problemset/problem/708/A 题目大意: 从字符串s中挑选出一个子串(非空),将该子串中的每个字母均替换成前一个字母,如' ...
- CodeForces 709C Letters Cyclic Shift (水题)
题意:给定一个字符串,让你把它的一个子串字符都减1,使得总字符串字典序最小. 析:由于这个题是必须要有一个字串,所以你就要注意这个只有一个字符a的情况,其他的就从开始减 1,如果碰到a了就不减了,如果 ...
- CodeForces 709C Letters Cyclic Shift
贪心. 肯定是两个$a$之间的那些字符都$-1$,没有$a$就全部$-1$.如果输入的串全是$a$,那么把最后一个$a$改成$z$. #pragma comment(linker, "/ST ...
- codeforces 709C C. Letters Cyclic Shift(贪心)
题目链接: C. Letters Cyclic Shift 题意: 现在一串小写的英文字符,每个字符可以变成它前边的字符即b-a,c-a,a-z这样,选一个字串变换,使得得到的字符串字典序最小; 思路 ...
- AIM Tech Round 3 (Div. 1) A. Letters Cyclic Shift 贪心
A. Letters Cyclic Shift 题目连接: http://www.codeforces.com/contest/708/problem/A Description You are gi ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- CF708A Letters Cyclic Shift 模拟
You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly ...
- Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift 水题
A. Hongcow Learns the Cyclic Shift 题目连接: http://codeforces.com/contest/745/problem/A Description Hon ...
- Largest Smallest Cyclic Shift
Largest Smallest Cyclic Shift 题目来源: Atcoder Code Festival 2017 Qual B Problem F 题目大意: 有\(X\)个字符'a',\ ...
随机推荐
- $(srctree) is not clean, please run 'make mrproper'
在使用make menuconfig重新配置后,再编译kernel时常会遇到这样的问题: Using /home/likewise-open/BJS/lisa.liu/code/sprdroid4.0 ...
- 求最长连续公共子序列 POJ 3080
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- Linux系统目录
[root@localhost ~]# ls /bin dev home lost+found misc opt root selinux sys usrboot etc lib media net ...
- Application_Start和Application_End事件执行时间
Application_start: 第一个访问网站的用户会触发该方法. 通常会在该方法里定义一些系统变量,如聊天室的在线总人数统计,历史访问人数统计的初始化等等均可在这里定义. Applicatio ...
- Linux下mysql数据库的命令
连接数据库命令:mysql -u 用户名 -p 密码 要求你输入要连接数据库的用户名和密码.用户名默认root密码不方便输入时,可以只输入:mysql -u 用户名 -p 然后回车,此时提示你输入密码 ...
- jqgrid的外观重绘
1.如果你想随时更改jqGrid的外观和列,可以先将jqGrid卸载掉再重新加载:$(grid).GridUnload(); $("#list_server_table").tr ...
- Photoshop安装
作者:郑超 参考地址:http://bbs.weiphone.com/read-htm-tid-4594713.html 下载地址:http://www.adobe.com/downloads.htm ...
- Android L(5.0)源码之手势识别onTouchEvent
onTouchEvent同样也是在view中定义的一个方法.处理传递到view 的手势事件.通过MotionEvent的getAction()方法来获取Touch事件的类型,类型包括ACTION_DO ...
- iOS开发——modifying the autolayout engine from a background thread
很多时候,我们需要用到多线程的东西,比如红外线检测是否有人经过.这种情况需要搞个子线程在后台不断的检测,这个线程可能是第三方提供的,你调用它给的方法,然后显示提示框的时候,问题就来了. 提示信息:Th ...
- iOS开发——Reachability和AFNetworking判断网络连接状态
一.Reachability // 监听网络状态改变的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selec ...