CF180D.Name

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Everything got unclear to us in a far away constellation Tau Ceti. Specifically, the Taucetians choose names to their children in a very peculiar manner.

Two young parents abac and bbad think what name to give to their first-born child. They decided that the name will be the permutation of letters of string ss . To keep up with the neighbours, they decided to call the baby so that the name was lexicographically strictly larger than the neighbour's son's name tt .

On the other hand, they suspect that a name tax will be introduced shortly. According to it, the Taucetians with lexicographically larger names will pay larger taxes. That's the reason abac and bbad want to call the newborn so that the name was lexicographically strictly larger than name tt and lexicographically minimum at that.

The lexicographical order of strings is the order we are all used to, the "dictionary" order. Such comparison is used in all modern programming languages to compare strings. Formally, a string pp of length nn is lexicographically less than string qq of length mm , if one of the two statements is correct:

  • n<m , and pp is the beginning (prefix) of string qq (for example, "aba" is less than string "abaa"),
  • p1=q1p_{1}=q_{1} , p2=q2p_{2}=q_{2} , ..., pk1=qk1p_{k-1}=q_{k-1} , p_{k}<q_{k} for some kk ( 1<=k<=min(n,m)1<=k<=min(n,m) ), here characters in strings are numbered starting from 1.

Write a program that, given string ss and the heighbours' child's name tt determines the string that is the result of permutation of letters in ss . The string should be lexicographically strictly more than tt and also, lexicographically minimum.

输入格式

The first line contains a non-empty string ss ( 1<=s<=50001<=|s|<=5000 ), where s|s| is its length. The second line contains a non-empty string tt ( 1<=t<=50001<=|t|<=5000 ), where t|t| is its length. Both strings consist of lowercase Latin letters.

输出格式

Print the sought name or -1 if it doesn't exist.

输入输出样例

  • 输入#1

    aad
    aac
    

    输出#1

    aad
    
  • 输入#2

    abad
    bob
    

    输出#2

    daab
    
  • 输入#3

    abc
    defg
    

    输出#3

    -1
    
  • 输入#4

    czaaab
    abcdef
    

    输出#4

    abczaa
    

说明/提示

In the first sample the given string ss is the sought one, consequently, we do not need to change the letter order there.

首页