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',\ ...
随机推荐
- FTP服务器中vsftpd主配置文件解析
/etc/vsftpd/vsftpd.conf#################匿名权限控制############### anonymous_enable=YES #是否启用匿名用户no_anon_ ...
- H3C inode for OSX 10.10 校园网客户端亲测可用
1.打开终端 2.输入以下命令 sudo /library/StartupItems/iNodeAuthService/iNodeAuthService start 3.输入管理密码 4.打开客户端联 ...
- PHPCMS v9 实现首页,列表页,内容页调用点击量方法
大家好,今天有点闲,看很多朋友经常问PHPCMS v9 首页,列表页,内容页调用点击怎么弄,打算抽时间把代码全部归纳出来,以便大家日后使用,如下: 1,首页调用点击量 {pc:content acti ...
- iOS 添加手机密码、指纹进行安全验证
为APP添加安全验证 1.导入头文件 #import <LocalAuthentication/LocalAuthentication.h> 2.添加手机密码验证 //创建安全验证对象 L ...
- November 11th 2016 Week 46th Friday
Keep in mind that neither success nor failure is ever final. 无论成败,皆非定局. The final is not coming, but ...
- linux之scp
传输文件夹 scp -r -P 目标端口号 文件夹名 目标用户名@目标服务器地址:目标存放地址 传输文件夹 scp -P 目标端口号 文件名 目标用户名@目标服务器地址:目标存放地址
- ios-Ineligible Devices 不被识别的设备
此问题大致分为几种: 1.设备不可用,出现Ineligible Devices,如下图: 此错误因为 Xcode的Deployment Target 大于设备的,选择和设备一样 或者 低于设备的.如下 ...
- struts2拦截器-自定义拦截器,放行某些方法(web.xml配置)
一.web.xml配置 <filter> <filter-name>encodingFilter</filter-name> <filter-class> ...
- php+jquery+ajax+json简单小例子
直接贴代码: <html> <title>php+jquery+ajax+json简单小例子</title> <?php header("Conte ...
- 安卓 SQLite error:SQLite database locked exception while compling : PRAGMA journal_mode .....
项目中频繁的切换Tab键,会频繁地从数据库读取数据,这是报出这样的错误: 解决方法是在国外的某论坛找到的 : 在插入数据的时候,首先: SQLiteDatabase db = SQLiteDataba ...