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;
}
最新文章
- IOS第18天(1,核心动画layer, 旋转,缩放,平移,边框,剪裁,圆角)
- 批处理中的echo命令图文详解
- 使用命令参数方式指定log4j配置文件
- Cent OS yum 安装 Adobe flash player
- tostring格式化输出
- javaweb学习总结十一(JAXP对XML文档进行DOM解析)
- C++STL学习笔记_(4)queue
- 对CLR异常和状态管理的一点理解
- php转义和去掉html、php标签函数
- 用script实现内容显示,并使用json传输数据
- CSS 去除浏览器默认 轮廓外框
- HDU_2027——统计元音
- iOS socket 实现tcp和服务器长链接的简单使用心得
- 安装、配置、启动FTP、SSH或NFS服务
- Spring中一个类的注入和引用是不一样的
- 使用Spring Boot快速构建基于SQLite数据源的应用
- 自动化测试-Selenium家谱介绍
- PLSQL 注册码
- markdown小知识总结
- python 解析命令行
热门文章
- 第一章计算机网络概述---OSI七层网络模型
- 【Linux】ethtool 用法
- 【Oracle】oracle pctfree和pctused详解
- [Usaco2009 Feb]Bullcow 牡牛和牝牛
- 【高并发】ReadWriteLock怎么和缓存扯上关系了?!
- js中常用追加元素的几种方法
- Vue使用Ref跨层级获取组件实例
- Devexpress DockManager多页面浮动窗口会关闭所有页面的问题
- Angular入门到精通系列教程(14)- Angular 编译打包 &; Docker发布
- Python_ 1生成器(上)初识生成器