您的位置:首页 > 其它

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,否则不存在

主要是有特别情况值得记录,比如:

输入:pnzcl

pnzdf

输出: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