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. ...
随机推荐
- 【Redis3.0.x】NoSql 入门
Redis3.0.x NoSql 入门 概述 NoSQL(Not Only SQL ),即不仅仅是 SQL,泛指非关系型的数据库.NoSQL 数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑 ...
- Java并发包源码学习系列:ReentrantReadWriteLock读写锁解析
目录 ReadWriteLock读写锁概述 读写锁案例 ReentrantReadWriteLock架构总览 Sync重要字段及内部类表示 写锁的获取 void lock() boolean writ ...
- docker 数据卷的挂载和使用
容器之间的数据共享技术, Docker容器产生的数据同步到本地 卷技术 --> 目录挂载, 将容器内的目录挂载到服务器上 使用命令来挂载 -v # 可以挂载多个目录 docker run -it ...
- ABP vNext 实现租户Id自动赋值插入
背景 在使用ABP vNext过程中,因为我们的用户体系庞大,所以一直与其他业务同时开发,在开发其他业务模块时,我们一直存在着误区:认为ABP vNext 自动处理了数据新增时的租户Id(Tenant ...
- uni-app开发经验分享十六:发布android版App的详细过程
开发环境 1. Android Studio下载地址:Android Studio官网 OR Android Studio中文社区 2. HBuilderX(开发工具) 3. App离线SDK下载:最 ...
- Azure Terraform(七)利用Azure DevOps 实现自动化部署基础资源(补充)
一,引言 之前一篇文章有讲解到利用 利用Azure DevOps 实现自动化部署基础资源,当时 TF 代码没有针对 Azure 各个资源的封装,所有的资源代码全部写在一个 main.tf 文件中.然后 ...
- ES 2021 来了,详细解读5个新特性,附案例
ES 2021是世界上最受欢迎的编程语言的最新版本〜 本次迭代中包含了五个新特性,让我们来一睹为快. 1.全部替换replaceAll: js默认的replace 方法仅替换字符串中一个模式的第一个实 ...
- https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth p.p ...
- High Performance Networking in Google Chrome 进程间通讯(IPC) 多进程资源加载
小结: 1. 小文件存储于一个文件中: 在内部,磁盘缓存(disk cache)实现了它自己的一组数据结构, 它们被存储在一个单独的缓存目录里.其中有索引文件(在浏览器启动时加载到内存中),数据文件( ...
- 极光推送的设备唯一性标识 RegistrationID
极光推送的设备唯一性标识 RegistrationID 极光推送的设备唯一性标识 RegistrationID | 极光博客 https://blog.jiguang.cn/registrationi ...