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
output
input
output
input
output
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代码:
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
相关文章推荐
- CF 584E(Anton and Ira-构造+贪心)
- CF - 805B. 3-palindrome - 贪心+构造
- CF - 805C. Find Amir - 贪心思维+构造
- CF - 733C 构造 + 贪心
- hdu 4982 贪心构造序列
- sdut 3903 CF(贪心+背包)
- CF - 797A. k-Factorization - 思维+贪心+数学
- CF - 807B. T-Shirt Hunt - 贪心+模拟
- URAL 1995 Illegal spices 贪心构造
- [CF805C] Find Amir(贪心,构造,模拟)
- CF 496E(Distributing Parts-序列混合贪心+set)
- hdu 5573贪心构造
- codeforce 509C (很有趣的贪心模拟构造)
- cf Learn from Life (简单贪心)
- CF:Card(贪心)
- CF - 779A. Pupils Redistribution 贪心+思维
- CF - 752B. Santa Claus and Keyboard Check 贪心+字符串
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp
- CF 861C Did you mean DP or 贪心
- CF 557B(Pasha and Tea-贪心)