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;
        }
    }
  • 相关阅读:
    JVM学习五:JVM之类加载器之编译常量和主动使用
    JVM学习一:JVM之类加载器概况
    JVM学习三:JVM之类加载器之连接分析
    JVM学习二:JVM之类加载器之加载分析
    JVM学习四:JVM之类加载器之初始化分析
    高斯滤波详解 附python和matlab高斯滤波代码
    图像最大池化
    图像平均池化 pytorch库中的平均池化
    图像色彩量化处理
    图像RGB到HSV色彩空间转换及其逆变换
  • 原文地址:https://www.cnblogs.com/GXXX/p/6814574.html
Copyright © 2011-2022 走看看