zoukankan      html  css  js  c++  java
  • 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!

    Input

    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.

    Output

    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.

    Sample test(s)
    input
    a
    c
    
    output
    b
    
    input
    aaa
    zzz
    
    output
    kkk
    
    input
    abcdefg
    abcdefh
    
    output
    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
    ",s);
    		else printf("No such string
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    我的本科毕业论文——Messar即时通讯系统
    你为什么不用Flash做程序的表示层呢?
    用于Blog的天气预报服务-改进20050806
    写了个小程序,方便大家编程(QuickDog,快捷键帮手)
    庆祝"上海.NET俱乐部"今天成立,请申请加入的朋友在这里Sign you name
    HTML+CSS+Javascript教学视频【0409更新】
    关于推迟7月9日上海.NET俱乐部第一次技术交流会的通知
    关于“上海.NET俱乐部”第一次技术交流会进展报告
    2005年8月13日 上海.NET俱乐部第一次活动纪实 已经发布,资料提供下载
    喜欢互联网行业,是因为它拥有着无穷的变数
  • 原文地址:https://www.cnblogs.com/herumw/p/9464850.html
Copyright © 2011-2022 走看看