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. ...
随机推荐
- 静默(命令行)安装oracle 11g
CentOS 6 静默安装oracle 11g 我参考的这个,他非常详细:https://blog.csdn.net/JIANG123456T/article/details/77745892 我只是 ...
- poj-DNA排序
描述 现在有一些长度相等的DNA串(只由ACGT四个字母组成),请将它们按照逆序对的数量多少排序. 逆序对指的是字符串A中的两个字符A[i].A[j],具有i < j 且 A[i] > A ...
- 【葵花宝典】kolla部署OpenStack-AllinOne
1.关闭防火墙以及内核安全机制 systemctl stop firewalld systemctl disable firewalld ##永久性关闭 setenforce 0 sed -i 's/ ...
- 入门OJ:Coin
题目描述 你有n个硬币,第i硬币面值为ai,现在总队长想知道如果丢掉了某个硬币,剩下的硬币能组成多少种价值?(0价值不算) 输入格式 第一行一个整数n 第二行n个整数.,a1,a2-an. 1< ...
- MariaDB(selec的使用)
--查询基本使用 -- 查询所有列 --select * from 表名 select * from students; --一定条件查询 select * from students whe ...
- Shell从入门到精通
熟悉基本shell操作不仅是运维的基本功,对于开发来说也是多多益善,我在学习的过程中,总结了十个练手的小demo,并附上涉及的知识点,仅供娱乐. 1. 多线程ping监控,检查同一网段的IP是否连通 ...
- 阿里云ECS hadoop+spark+zookeeper+hive code-server 集群搭建
懒得重新排版然后发到博客了.用在线文档看吧 https://www.kdocs.cn/l/srV6o8rABW9V 用线上IDE(code-server)写scala的时候,出现BUG可以参考下面两篇 ...
- git的使用学习笔记---合并分支
一.使用场景 不同的分支需要合并 二.操作 1.首先要创建一个分支 git checkout -b mergedemo 创建文本 vim text.txt 添加文本 git add text.txt ...
- 研发流程 接口定义&开发&前后端联调 线上日志观察 模型变动
阿里等大厂的研发流程,进去前先了解一下_我们一起进大厂 - SegmentFault 思否 https://segmentfault.com/a/1190000021831640 接口定义 测试用例评 ...
- (Oracle)常用的数据库函数
Trim: Trim() 函数的功能是去掉首尾空格. Eg: trim(to_char(level, '00')) Trunc: 1.TRUNC函数为指定元素而截去的日期值. trun ...