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;
        }
    }
  • 相关阅读:
    用javafx webview 打造自己的浏览器
    用cef Python打造自己的浏览器
    无需人工智能和机器学习,实现基于手势识别的计算器
    经常抱怨在公司学不到技术,学的技术没有使用场景怎么破?
    Linux相关集合
    个人博客注册,申请,美化流程
    Django注意知识点(二)
    Django 注意知识点(一)
    Java学习笔记(四)
    Java学习笔记(三)
  • 原文地址:https://www.cnblogs.com/GXXX/p/6814574.html
Copyright © 2011-2022 走看看