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;
    }
    
  • 相关阅读:
    Elasticsearch崩溃解决办法
    阿里云服务器ubuntu14.04安装Redis
    error: command ‘x86_64-linux-gnu-gcc’ failed with exit status 1
    本机访问阿里云服务器上的Elasticsearch
    阿里云服务器配置ElasticSearch
    ElasticSearch-RTF的安装
    【已解决】neo4j-import使用过程中遇到的问题(there's a field starting with a quote and whereas it ends that quote there seems to be characters in that field /Executor has been shut down in panic)
    OutOfMemoryError和StackOverflowError
    线程的终止
    scala基础
  • 原文地址:https://www.cnblogs.com/herumw/p/9464850.html
Copyright © 2011-2022 走看看