AIM Tech Round 3 (Div. 2) C. Letters Cyclic Shift 贪心、字典序
来源:互联网 发布:python netsnmp安装 编辑:程序博客网 时间:2024/06/16 04:15
You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of sand 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 thats1 = t1, s2 = t2, ..., si - 1 = ti - 1, and si < ti.
Source
AIM Tech Round 3 (Div. 2)
My Solution
贪心、字典序
必须改一个子串使得得到的新串字典序最小
所以从左往右改第一个不是a的, 然后连着的都要改直到碰到一个a为止
但是exactly one non-empty substring
所以全是a的时候也要改, 把最后一个a改成 z
#include <iostream>#include <cstdio>#include <string>using namespace std;typedef long long LL;const int maxn = 1e6 + 8;string s;int main(){ #ifdef LOCAL freopen("c.txt", "r", stdin); //freopen("o.txt", "w", stdout); int T = 3; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); cin>>s; int sz = s.size(); bool flag = false; char ch; for(int i = 0; i < sz; i++){ if(!flag){ if(s[i] == 'a') ; else{ flag = true; ch = s[i]; s[i] = (ch - 1); } } else{ if(s[i] == 'a') break; else{ ch = s[i]; s[i] = (ch - 1); } } } if(!flag){ s[sz - 1] = 'z'; } cout<<s<<endl; #ifdef LOCAL cout<<endl; } #endif // LOCAL return 0;}
Thank you!
------from ProLights
- AIM Tech Round 3 (Div. 2) C. Letters Cyclic Shift 贪心、字典序
- AIM Tech Round 3 (Div. 2) C.Letters Cyclic Shift
- Codeforces Aim Tech Round 3 (Div.2 )C.Letters Cyclic Shift 【贪心】水题
- AIM Tech Round 3 (Div. 2) C. Letters Cyclic Shift (字符串)
- AIM Tech Round 3 (Div. 1) A. Letters Cyclic Shift 贪心
- AIM Tech Round 3 (Div. 2) C(贪心)
- AIM Tech Round 4 (Div. 2) C
- CF708A(AIM Tech Round 3 (Div. 2) - C)
- AIM Tech Round 3 (Div. 2)
- AIM Tech Round 3 (Div. 2)
- AIM Tech Round 3 (Div. 2)
- 【codeforces】AIM Tech Round 3 (Div. 2)
- codeforces AIM Tech Round 3 (Div. 2)
- AIM Tech Round (Div. 2) C. Graph and String(二分图染色 | 贪心)
- AIM Tech Round 3 (Div. 2) D. Recover the String 构造、贪心、多坑、WA162
- AIM Tech Round 3 (Div. 1) C. Centroids(树形DP)
- AIM Tech Round 3 (Div. 1)-C. Centroids
- AIM Tech Round (Div. 2) C. Graph and String
- 利用Matlab设计滤波器(FDAT)
- 39.超文本标记语言HTML详解(下)
- java笔记--day09--抽象类(二)
- 决策树
- 51nod-1065
- AIM Tech Round 3 (Div. 2) C. Letters Cyclic Shift 贪心、字典序
- ssh整合-[Ljava.lang.String Date类型存入出现异常解决
- BZOJ1828: [Usaco2010 Mar]balloc 农场分配 贪心+线段树
- 常用linux命令(java开发)
- Java中的float和long
- django创建一个项目
- Hibernate之多对一
- 算法--02.插入排序
- 效果展示的总结