Problem
The Little Elephant loves lucky strings. Everybody knows that the lucky string is a string of digits that contains only the lucky digits 4 and 7. For example, strings "47", "744", "4" are lucky while "5", "17", "467" are not.
The Little Elephant has the strings A and B of digits. These strings are of equal lengths, that is |A| = |B|. He wants to get some lucky string from them. For this he performs the following operations. At first he arbitrary reorders digits of A. Then he arbitrary reorders digits of B. After that he creates the string C such that its i-th digit is the maximum between the i-th digit of A and the i-th digit of B. In other words, C[i] = max{A[i], B[i]} for i from 1 to |A|. After that he removes from C all non-lucky digits saving the order of the remaining (lucky) digits. So C now becomes a lucky string. For example, if after reordering A = "754" and B = "873", then C is at first "874" and then it becomes "74".
The Little Elephant wants the resulting string to be as lucky as possible. The formal definition of this is that the resulting string should be the lexicographically greatest possible string among all the strings that can be obtained from the given strings A and B by the described process.
Notes
- |A| denotes the length of the string A.
- A[i] denotes the i-th digit of the string A. Here we numerate the digits starting from 1. So 1 ≤ i ≤ |A|.
- The string A is called lexicographically greater than the string B if either there exists some index i such that A[i] > B[i] and for each j < i we have A[j] = B[j], or B is a proper prefix of A, that is, |A| > |B| and first |B| digits of A coincide with the corresponding digits of B.
Input
The first line of the input contains a single integer T, the number of test cases. T test cases follow. Each test case consists of two lines. The first line contains the string A. The second line contains the string B.
Output
For each test case output a single line containing the answer for the corresponding test case. Note, that the answer can be an empty string. In this case you should print an empty line for the corresponding test case.
Constraints
1 ≤ T ≤ 10000
1 ≤ |A| ≤ 20000
|A| = |B|
Each character of A and B is a digit.
Sum of |A| across all the tests in the input does not exceed 200000.
Sample 1:
4 4 7 435 479 7 8 1675475 9756417
7 74777744
Explanation:
Case 1. In this case the only possible string C we can get is "7" and it is the lucky string.
Case 2. If we reorder A and B as A = "543" and B = "749" the string C will be at first "749" and then becomes "74". It can be shown that this is the lexicographically greatest string for the given A and B.
Case 3. In this case the only possible string C we can get is "8" and it becomes and empty string after removing of non-lucky digits.
Case 4. If we reorder A and B as A = "7765541" and B = "5697714" the string C will be at first "7797744" and then becomes "777744". Note that we can construct any lexicographically greater string for the given A and B since we have only four "sevens" and two "fours" among digits of both strings A and B as well the constructed string "777744".
Code(C++):-
Code(JAVA):-
import java.util.*;import java.lang.*;import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */class Codechef{ public static void main (String[] args) throws java.lang.Exception { StringBuilder result = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine(); int T = Integer.parseInt(s); for (int t=0; t<T; t++) { char[] A = reader.readLine().toCharArray(); int a03 = 0; int a4 = 0; int a56 = 0; int a7 = 0; int a89 = 0; for (char c : A) { switch (c) { case '0': case '1': case '2': case '3': a03++; break; case '4': a4++; break; case '5': case '6': a56++; break; case '7': a7++; break; case '8': case '9': a89++; break; } } char[] B = reader.readLine().toCharArray(); int b03 = 0; int b4 = 0; int b56 = 0; int b7 = 0; int b89 = 0; for (char c : B) { switch (c) { case '0': case '1': case '2': case '3': b03++; break; case '4': b4++; break; case '5': case '6': b56++; break; case '7': b7++; break; case '8': case '9': b89++; break; } } int sevens = 0; int x = Math.min(a7, b56); sevens += x; a7 -= x; b56 -= x; x = Math.min(a7, b03); sevens += x; a7 -= x; b03 -= x; x = Math.min(a7, b4); sevens += x; a7 -= x; b4 -= x; x = Math.min(b7, a7); sevens += x; b7 -= x; a7 -= x; x = Math.min(b7, a56); sevens += x; b7 -= x; a56 -= x; x = Math.min(b7, a03); sevens += x; b7 -= x; a03 -= x; x = Math.min(b7, a4); sevens += x; b7 -= x; a4 -= x; int fours = 0; x = Math.min(a4, b03); fours += x; a4 -= x; b03 -= x; x = Math.min(b4, a4); fours += x; b4 -= x; a4 -= x; x = Math.min(b4, a03); fours += x; b4 -= x; a4 -= x; for (int i=0; i<sevens; i++) { result.append("7"); } for (int i=0; i<fours; i++) { result.append("4"); } result.append("\n"); } System.out.print(result); }}
Recommended Post :-
- Swap the adjacent characters of the string
- Double the vowel characters in the string
- Character with their frequency
- Program to find the closest value
- Swap adjacent characters
- Double the vowel characters
- Check valid parenthesis
- Print the characters with their frequencies
- Find closest value
- Word Count
- Program of CaesarCipher
- Program to find the perfect city
- Annual Day | Tech Mahindra coding question
- Find the number of pairs in the array whose sum is equal to a given target.
Full C course:-
Key points:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
- How to check that given number is power of 2 or not
- How to fix limit in double and floating numbers after dot (.) in c++
- How to print a double or floating point number in scientific notation and fixed notation
- How to take input a string in c
- How to reduce the execution time of program in c++.
Cracking the coding interview:-
Array and string:-
Tree and graph:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Sum of Digits of a Five Digit Number | hackerrank practice problem solution
- 1D Arrays in C | hackerrank practice problem solution
- Array Reversal | hackerrank practice problem solution
- Printing Tokens | hackerrank practice problem solution
- Digit Frequency | hackerrank practice problem solution
- Calculate the Nth term | hackerrank practice problem solution
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
MCQs:-
0 Comments