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: Runtime Error)

import Data.List hiding (find)
import Data.Tree
--import Data.Array
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.ST
--import Data.Vector as V
import Control.Monad
import Control.Monad.ST
import Control.Monad.State
import Debug.Trace

type Str = UArray Int Char
--type Table = UArray (Int,Int) Int --sl*tl matrix
type DP s = STUArray s (Int,Int) Int
type DPTable = UArray (Int,Int) Int -- == freeze $ DP s

dp_i :: DP s -> Str -> Str -> Int -> Int -> ST s ()
dp_i dparr s t i j = do
   dpij   <- readArray dparr (i,j)
   dpi1j1 <- readArray dparr (i-1,j-1)
   dpi1j  <- readArray dparr (i-1,j)
   dpij1  <- readArray dparr (i,j-1)
   if s!i == t!j then writeArray dparr (i,j) (max (dpi1j1+1) dpi1j1)
     else return ()
   writeArray dparr (i,j-1) (max dpi1j1 dpij1)
   writeArray dparr (i-1,j) (max dpi1j1 dpi1j)

--DPTableからStringを復元
mkans :: DPTable -> Str -> Int -> Int -> String
mkans dparr s i j
  | i == 0 || j == 0 = []
  | dparr!(i,j) == dparr!(i-1,j-1) = mkans dparr s (i-1) (j-1)
  | dparr!(i,j) == dparr!(i-1,j-1) + 1 =
    if dparr!(i,j) == dparr!(i-1,j) then mkans dparr s (i-1) j
    else if dparr!(i,j) == dparr!(i,j-1) then mkans dparr s i (j-1)
      else s!i : (mkans dparr s (i-1) (j-1))

solve :: Str -> Str -> Int -> Int -> ST s String
solve s t sl tl = do
  dparr <- newArray ((0,0),(sl,tl)) 0
  forM_ [1..sl] $ \i ->
    forM_ [1..tl] $ \j -> dp_i dparr s t i j
  dparr' <- freeze dparr
  let ans = reverse $ mkans dparr' s sl tl
  return ans

main :: IO ()
main = do
  s <- getLine
  t <- getLine
  let sl = length s
      tl = length t
      s' = listArray (1,sl) s
      t' = listArray (1,tl) t
  putStrLn $ runST $ solve s' t' sl tl