The Solution of UESTC 2016 Summer Training #1 Div.2 Problem B

Link

http://acm.hust.edu.cn/vjudge/contest/121539#problem/B

Description

standard input/output 

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?

Input

The 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.

Output

Print string A on the first line and string B on the second line. Both strings should contain only lowercase English letters.

Sample Input

Input
3 4
0 0 0 0 0
0 0 1 1 1
0 0 1 1 2
0 1 1 1 2
Output
abc
cadb

Solution

First of all,we should initialize the two strings.We can find that R and C are less than or equal to 25,which means the length of string A and string B are less than or equal to 25.There are 26 letters in alphabet.So we can initialize the string A to "abcdef...wxy" and initialize the string B to "zzz...zzz".From the pseudo-code "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])" we know that if DP[i,j] is different from DP[i-1,j-1],DP[i,j-1] and DP[i-1,j],A[i] must be equal to B[j].So,we can replace B[j] to A[i].But sometimes we may find B[j] has been replaced to a certain letter.In this case,we are supposed to replace all the letters which equal to A[i] in string A and string B to B[j].By scanning the matrix once,we can get the correct answer.

C++ Code

 

#include<iostream>
using namespace std;
int main()
{
  int i,j,k,r,c;
  int a[30][30];
  char s1[30],s2[30],s;
  cin>>r>>c;
  for(i=0;i<=r;i++)
   for(j=0;j<=c;j++)
    cin>>a[i][j];
  for(i=1;i<=r;i++)
    s1[i]=i+96;
  for(i=1;i<=c;i++)
    s2[i]='z';
  for(i=1;i<=r;i++)
   for(j=1;j<=c;j++)
    if (a[i][j]!=a[i-1][j-1] && a[i][j]!=a[i-1][j] && a[i][j]!=a[i][j-1])
     if (s2[j]=='z') s2[j]=s1[i];
     else
     {
        s=s1[i];
        for(k=1;k<=r;k++)
         if (s1[k]==s) s1[k]=s2[j];
        for(k=1;k<=c;k++)
         if (s2[k]==s) s2[k]=s2[j];
     }
  for(i=1;i<=r;i++)
    cout<<s1[i];
  cout<<endl;
  for(i=1;i<=c;i++)
    cout<<s2[i];
  cout<<endl;
  return 0;
}

原文地址:https://www.cnblogs.com/cs-lyj1997/p/5712885.html