您的位置:首页 > 产品设计 > UI/UE

CF - 801B. Valued Keys - 构造+贪心

2017-04-20 19:56 246 查看
1.题目描述:

B. Valued Keys

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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
the i-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.

Examples

input
ab
aa


output
ba


input
nzwzl
niwel


output
xiyez


input
ab
ba


output
-1


Note

The first case is from the statement.

Another solution for the second case is "zizez"

There is no solution for the third case. That is, there is no z such that f("ab", z) =  "ba".

2.题意概述:

定义函数f(x,y)=z其中xyz都是字符串,z第i个字符等于字典序中x[i]和y[i]中最小的那个字符,现在给你x和z,问你能否构造y串?

3.解题思路:

对于z[i],如果z[i]比x[i]大,那肯定是不合法的,因为既然x[i] ,< f(x, y),那么z[i]要么是x[i],要么是比x[i]字典序还小的y[i],不合法。

否则就贪心地构造y[i] = x[i],这样一定可以保证z[i]是在x[i]中取。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
char x
, y
, z
;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
while (~scanf("%s%s", x, y))
{
int len = strlen(x);
bool flag = 1;
for (int i = 0; i < len; i++)
if (y[i] <= x[i])
z[i] = y[i];
else
{
flag = 0;
break;
}
if (flag)
{
z[len] = '\0';
puts(z);
}
else
puts("-1");

}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
close
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm 算法 codeforces