Problem:
Score : 100 points

Problem Statement

You are given strings s and t.
Find one longest string that is a subsequence of both s and t.

Notes

A subsequence of a string x is the string obtained by removing zero or more characters from x and concatenating the remaining characters without changing the order.

Constraints

s and t are strings consisting of lowercase English letters.

1 \leq |s|, |t| \leq 3000

Input

Input is given from Standard Input in the following format:

s
t

Output

Print one longest string that is a subsequence of both s and t.
If there are multiple such strings, any of them will be accepted.

Sample Input 1

axyb
abyxb

Sample Output 1

axb

The answer is axb or ayb; either will be accepted.

Sample Input 2

aa
xayaz

Sample Output 2

aa

Sample Input 3

a
z

Sample Output 3

The answer is (an empty string).

Sample Input 4

abracadabra
avadakedavra

Sample Output 4

aaadara

Solution in Scala (2.11.7) (judge verdict: Accepted)

object Main {

  def main(args: Array[String]): Unit = {
    val sc = new java.util.Scanner(System.in)

    val S = sc.nextLine()
    val T = sc.nextLine()

    val dp = Array.fill(S.length+1, T.length+1)(0)
    val dpi = Array.fill(S.length+1, T.length+1)(-1)
    val dpj = Array.fill(S.length+1, T.length+1)(-1)

    var i = 0
    while (i < S.length) {
      var j = 0
      while (j < T.length) {
        if (S(i) == T(j)) {
          if ((dp(i)(j) + 1) > dp(i + 1)(j + 1)) {
            dp(i + 1)(j + 1) = dp(i)(j) + 1
            dpi(i + 1)(j + 1) = i
            dpj(i + 1)(j + 1) = j
          }
        }
        if (dp(i + 1)(j) > dp(i + 1)(j + 1)) {
          dp(i + 1)(j + 1) = dp(i + 1)(j)
          dpi(i + 1)(j + 1) = dpi(i + 1)(j)
          dpj(i + 1)(j + 1) = dpj(i + 1)(j)
        }
        if (dp(i)(j + 1) > dp(i + 1)(j + 1)) {
          dp(i + 1)(j + 1) = dp(i)(j + 1)
          dpi(i + 1)(j + 1) = dpi(i)(j + 1)
          dpj(i + 1)(j + 1) = dpj(i)(j + 1)
        }
        j += 1
      }
      i += 1
    }

    i = dpi(S.length)(T.length)
    var j = dpj(S.length)(T.length)
    var bldr = new StringBuilder
    while (i >= 0 || j >= 0) {
      if (S(i) == T(j))
        bldr.append(S(i))
      val _i = dpi(i)(j)
      val _j = dpj(i)(j)
      i = _i
      j = _j
    }
    println(bldr.result().reverse)
  }

}