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;
        }
    }
  • 相关阅读:
    android使用wcf接收上传图片视频文件
    android获取时间差的方法
    android JSON 技术
    Android LogCat使用详解
    VS2013菜单栏文字全大写的问题
    Mysql5.7安装配置
    解决Android Studio Gradle Build Running慢的问题
    Mysql创建用户并授权
    Windows 7 常用快捷键
    Python CRC16校验算法
  • 原文地址:https://www.cnblogs.com/GXXX/p/6814574.html
Copyright © 2011-2022 走看看