zoukankan      html  css  js  c++  java
  • E

    E - Valued Keys

    You found a mysterious function f. The function takes two strings s1 and s2. These strings must consist only of lowercase English letters, and must be the same length.

    The output of the function f is another string of the same length. The i-th character of the output is equal to the minimum of the i-th character of s1 and thei-th character of s2.

    For example, f("ab", "ba") = "aa", and f("nzwzl", "zizez") = "niwel".

    You found two strings x and y of the same length and consisting of only lowercase English letters. Find any string z such that f(x, z) = y, or print -1 if no such string z exists.

    Input

    The first line of input contains the string x.

    The second line of input contains the string y.

    Both x and y consist only of lowercase English letters, x and y have same length and this length is between 1 and 100.

    Output

    If there is no string z such that f(x, z) = y, print -1.

    Otherwise, print a string z such that f(x, z) = y. If there are multiple possible answers, print any of them. The string z should be the same length as x and y and consist only of lowercase English letters.

    Example

    Input
    ab
    aa
    Output
    ba
    Input
    nzwzl
    niwel
    Output
    xiyez
    Input
    ab
    ba
    Output
    -1
    题意:输入两个长度相等的小写字符串x,y,能否找到字符串z,从第一个字符到最后一个字符,比较x,z,把最小的字符给y,即f(x, z) = y;如果z存在,就输出,否则输出-1;
    题解:分类讨论:1.当x[i]=y[i] z[i]=x[i];
             2.当x[i]<y[i] z[i]不存在;
             3.当x[i]>y[i] z[i]=y[i];
    代码
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    int main()
    {
        string str1,str2;
        char ans[105];
        int i,j,len,t;
        while(cin>>str1>>str2){
            t=1;
            len=str1.size();
            for(i=0;i<len;i++)
            {
                if(str1[i]==str2[i])
                {
                    ans[i]=str1[i];
                }
                else
                {
                    if(str1[i]<str2[i])
                    {
                        t=0;
                        break;
                    }
                    else
                    {
                        ans[i]=str2[i];
                    }
                }
            }
            if(t==0)
                cout<<-1;
            else
                {
                    for(i=0;i<len;i++)
                        printf("%c",ans[i]);
                }
                cout<<endl;
        }
    }
  • 相关阅读:
    【Objective-C学习笔记】变量和基本的数据类型
    数据文件实时同步(rsync + sersync2)
    入园自述
    软件工程人才的社会需求现状与发展趋势分析
    ERP-安心卡功能
    银盒宝成APP下载地址
    关于新商家后台添加商品后,一体机上不显示分类问题解决流程
    各类型商户微信认证方法
    网商微信实名认证FAQ
    扫码下单支持同桌单人点餐FAQ
  • 原文地址:https://www.cnblogs.com/GXXX/p/6814574.html
Copyright © 2011-2022 走看看