codeforces 518 A. Vitaly and Strings
2018-01-29 00:38
330 查看
codeforces 518 A
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t.
Let’s help Vitaly solve this easy problem!
题目大意:
寻找一个字典序大于s且小于t的字符串( s < t )数据范围:
|s| = |t| <= 100 (这么明显用暴力即可)解题思路:
从后往前遍历s寻找第一个不为’z’的s[i]。如果s[i] == ‘z’,就令s[i] = ‘a’;如果s[i] != ‘z’,就将该字符ASCII加一并退出循环,此时s一定大于原s,如果s != t就输出s,否则不存在
主要是有特别情况值得记录,比如:
输入:pnzclpnzdf
输出:pnzcm
本题代码:
#include <cstdio> #include <cstring> #include <iostream> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <algorithm> using namespace std; string s, t; int main() { cin >> s >> t; int len = s.size(); bool flag = 0; for(int i = len - 1; i >= 0; i--) { if(s[i] != 'z') { s[i]++; break; } if(s[i] == 'z') s[i] = 'a'; } if(s != t) cout << s << endl; else cout << "No such string" << endl; return 0; }
相关文章推荐
- codeforces 293 dvi2 A. Vitaly and Strings
- 4000 codeforces 296B - Yaroslav and Two Strings (DP+容斥)
- CodeForces 518B. Tanya and Postcard
- Codeforces Round #293 (Div. 2) A. Vitaly and Strings
- CodeForces 518 D. Ilya and Escalator(概率DP)
- ACdream 1056 Vitaly and Strings
- Codeforces Round #293 (Div. 2)——A—— Vitaly and Strings
- Codeforces 557D - Vitaly and Cycle (DFS染色)
- CF Vitaly and Strings
- 【CodeForces】A. Petya and Strings
- 518A - Vitaly and Strings【string】
- Codeforces 360C - Levko and Strings 组合数学+DP
- codeforces 288A:Polo the Penguin and Strings
- Codeforces518 D. Ilya and Escalator
- Codeforces 476 E. Dreamoon and Strings
- codeforces 518 C. Anya and Smartphone
- Vitaly and Strings
- Codeforces 112A-Petya and Strings(实现)
- 【34.57%】【codeforces 557D】Vitaly and Cycle
- CodeForces - 798B:Mike and strings