403064: GYM100989 B LCS (B)
Description
Although Haneen was able to solve the LCS problem, Dr. Ibrahim is suspicious about whether she really understands the LCS problem or not. He believes that she can't write the code on her own, but can only translate the LCS pseudo-code given in class into C++ code without really understanding how it works. Here is the pseudo-code Dr. Ibrahim gave in class:
function LCS (A[1..R], B[1..C])
DP = array(0..R, 0..C)
for i := 0..R
DP[i,0] = 0
for j := 0..C
DP[0,j] = 0
for i := 1..R
for j := 1..C
if A[i] = B[j]
DP[i,j] := DP[i-1,j-1] + 1
else
DP[i,j] := max(DP[i,j-1], DP[i-1,j])
return DP[R,C]
To verify that Haneen understands the LCS problem, Dr. Ibrahim asked her to solve the following problem:
After running the above LCS code on two strings A and B, the 2D array DP is filled with values. Given the 2D array DP, can you guess what A and B are? Any two strings A and B that will produce the given table and contain only lowercase English letters are acceptable.
Can you help Haneen solve this problem?
InputThe first line of input contains two integers R and C (1 ≤ R, C ≤ 25), the length of the strings A and B, respectively.
Each of the following R + 1 lines contains C + 1 integers, these lines represent the 2D array DP.
It's guaranteed that the given table was produced by running the algorithm on two strings that contain only lowercase English letters.
OutputPrint string A on the first line and string B on the second line. Both strings should contain only lowercase English letters.
ExampleInput3 4Output
0 0 0 0 0
0 0 1 1 1
0 0 1 1 2
0 1 1 1 2
abc
cadb