A. Vitaly and Strings
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!
The first line contains string s (1 ≤ |s| ≤ 100),
consisting of lowercase English letters. Here, |s| denotes the length of the string.
The second line contains string t (|t| = |s|),
consisting of lowercase English letters.
It is guaranteed that the lengths of strings s and t are
the same and string s is lexicographically less than string t.
If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).
If such string exists, print it. If there are multiple valid strings, you may print any of them.
a
c
b
aaa
zzz
kkk
abcdefg
abcdefh
No such string
做了很长时间一直WA,主要是考虑不充分。先考虑末尾的情况,然后如果前面对应第i个字符中第二串比第一串大于等于2就可以直接使得s[i]=s1[i]+1,然后其他的用'z'补齐,如果前面对应第i个字符中第二串比第一串大1就要分两种情况讨论,第一种是看s1中剩下字符是不是都为'z',只要一个不是,s[i]=s1[i],i后面的都用'z'补齐并输出。如果这种情况不满足,那么就看s2中是不是所有的字符都是'a',如果有一个不是,s[i]=s2[i],i后面的都用'a'补齐并输出。
#include<stdio.h>
#include<string.h>
char s1[200],s2[200],s[200];
int main()
{
int n,m,i,j,len,flag,flag1,flag2,flag3;
while(scanf("%s%s",s1,s2)!=EOF)
{
len=strlen(s1);
flag=1;
flag1=0;
memset(s,0,sizeof(s));
for(i=0;i<len;i++){
if(i==len-1 && s2[i]-s1[i]<=1){
flag=0;break;
}
else if(i==len-1 && s2[i]-s1[i]>=2){
s[i]=s1[i]+1;break;
}
else if(s2[i]-s1[i]>=2){
s[i]=s1[i]+1;
for(j=i+1;j<len;j++){
s[j]='z';
}
break;
}
else if(s2[i]-s1[i]==1){
flag2=0;
for(j=i+1;j<len;j++){
if(s1[j]!='z'){
flag2=1;break;
}
}
if(flag2==1){
s[i]=s1[i];
for(j=i+1;j<len;j++){
s[j]='z';
}
break;
}
else if(flag2==0){
flag3=0;
for(j=i+1;j<len;j++){
if(s2[j]!='a'){
flag3=1;break;
}
}
if(flag3==0){
flag=0;break;
}
else{
s[i]=s2[i];
for(j=i+1;j<len;j++){
s[j]='a';
}
break;
}
}
} else if(s2[i]==s1[i]){
s[i]=s1[i];continue;
}
else if(s1[i]>s2[i]){
flag=0;break;
} }
if(flag==1)printf("%s\n",s);
else printf("No such string\n");
}
return 0;
}
A. Vitaly and Strings的更多相关文章
- CF Vitaly and Strings
Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- codeforces 518A. Vitaly and Strings
A. Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #293 (Div. 2) A. Vitaly and Strings
A. Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standar ...
- CodeForces 518A Vitaly and Strings (水题,字符串)
题意:给定两个相同长度的字符串,让你找出一个字符串,字典序在两都之间. 析:这个题当时WA了好多次,后来才发现是这么水,我们只要把 s 串加上,然后和算数一样,该进位进位,然后再和 t 比较就行. 代 ...
- Codeforces Round #293 (Div. 2)
A. Vitaly and Strings 题意:两个字符串s,t,是否存在满足:s < r < t 的r字符串 字符转处理:字典序排序 很巧妙的方法,因为s < t,只要找比t字典 ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
随机推荐
- Vue css过渡 和 js 钩子过渡
css过渡 <transition name="slide"> <div v-show="!showChatInput" class=&quo ...
- 【MyBatis】MyBatis 连接池和事务控制
MyBatis 连接池和事务控制 文章源码 MyBaits 连接池 实际开发中都会使用连接池,因为它可以减少获取连接所消耗的时间.具体可查看 MyBatis 数据源配置在 SqlMapConfig.x ...
- AI智能皮肤测试仪助力美业数字化营销 实现门店与用户双赢局面
当皮肤遇到AI智能,会有怎么样的火花呢?随着生活水平的提升,人们对肌肤保养护理的需求也越来越高,人要美,皮肤养护也要更精准,数字化必将成为美业发展的新契机.新机遇下肌肤管家SkinRun为美业客户提供 ...
- zabbix客户端安装配置
1.下载,解压并安装zabbixtar zxvf zabbix-2.0.12.tar.gzcd zabbix-2.0.12./configure --prefix=/usr/local/zabbix ...
- Can't locate Time/HiRes.pm in @INC (@INC contains
Can't locate Time/HiRes.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/ ...
- bash shell关联数组总结
[原创]本博文为原创博文,引用或转发请注明原始出处和链接:https://www.cnblogs.com/dingbj/p/dict_array.html 什么是关联数组? 关联数组相对于索引数组,又 ...
- [Usaco2008 Mar]Cow Travelling游荡的奶牛
题目描述 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John在某个时刻看见 ...
- 错误捕捉过滤器 .NetCore版
前言 继承ExceptionFilterAttribute后,重写OnException函数. 统一捕捉所有报错,格式化返回前端. 代码实现 基类控制器 在基类控制器上添加[ErrorCatch]特性 ...
- ESPNet/ESPNetV2:空洞卷积金字塔 | 轻量级网络
ESPNet系列的核心在于空洞卷积金字塔,每层具有不同的dilation rate,在参数量不增加的情况下,能够融合多尺度特征,相对于深度可分离卷积,深度可分离空洞卷积金字塔性价比更高.另外,HFF的 ...
- 【Soul网关探秘】http数据同步-Admin通知前处理
引言 本篇开始研究 Soul 网关 http 数据同步,将分为三篇进行分析: <Admin通知前处理> <变更通知机制> <Bootstrap处理变更通知> 希望三 ...