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 Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Data.Array.Unboxed
import Data.Array.ST
import qualified Data.ByteString.Char8 as BS

makeTable :: BS.ByteString -> BS.ByteString -> UArray (Int,Int) Int
makeTable xs ys =
  runSTUArray $ do
    dp <- newArray ((0,0),(BS.length xs, BS.length ys)) 0
    forM_ [0..BS.length xs -1] $ \i ->
      forM_ [0..BS.length ys - 1] $ \j ->
        if BS.index xs i == BS.index ys j 
           then do
             rd <- readArray dp (i,j)
             writeArray dp (i+1,j+1) $! rd+1 
           else do
             rd1 <- readArray dp (i+1,j)
             rd2 <- readArray dp (i,j+1)
             writeArray dp (i+1,j+1) $! max rd1 rd2
    return dp

construct :: UArray (Int,Int) Int -> BS.ByteString -> BS.ByteString -> (Int,Int) -> BS.ByteString -> BS.ByteString
construct dp xs ys (n,m) bs
  | n == 0 || m == 0 = bs
  | BS.index xs (n-1) == BS.index ys (m-1) = construct dp xs ys (n-1,m-1) (BS.cons (BS.index xs (n-1)) bs)
  | otherwise =
    let
      a = dp ! (n-1,m)
      b = dp ! (n,m-1)
    in
      if a >= b then construct dp xs ys (n-1,m) bs
                else construct dp xs ys (n,m-1) bs
main = do 
  xs <- BS.getLine
  ys <- BS.getLine
  let n = BS.length xs
  let m = BS.length ys
  let dp = makeTable xs ys
  putStrLn $ BS.unpack $ construct dp xs ys (n,m) BS.empty