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: Wrong Answer)

{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Data.Int
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Unboxed.Mutable as VM
import qualified Data.ByteString.Char8 as BS
import Data.Array.Unboxed
import Data.Array.ST
import Control.Monad.ST

asSTUArray :: ST s (STUArray s i x) -> ST s (STUArray s i x)
asSTUArray x = x

-- Input: s
-- Output: arr
--   forall i x. let j = arr!(i,x) in j == -1 || (s `BS.index` j == x && all (\k -> s `BS.index` k /= x) [i..j-1])
indexTable :: BS.ByteString -> UArray (Int,Char) Int
indexTable s = runSTUArray $ do
  let n = BS.length s
  arr <- newArray ((0,'a'),(n - 1,'z')) (-1)
  t <- asSTUArray $ newArray ('a','z') (-1)
  forM_ [n-1,n-2..0] $ \i -> do
    let x = BS.index s i
    writeArray t x i
    forM_ ['a'..'z'] $ \y -> do
      j <- readArray t y
      writeArray arr (i,y) j
  return arr

lcsTable :: BS.ByteString -> BS.ByteString -> UArray (Int,Int) Int
lcsTable s t = runSTUArray $ do
  let m = BS.length s
      n = BS.length t
      t_tbl = indexTable t
  arr <- newArray ((0,0),(m,n)) 0
  forM_ [m-1,m-2..0] $ \i -> do
    let !x = BS.index s i
    forM_ [n-1,n-2..0] $ \j -> do
      l0 <- readArray arr (i+1, j)
      let k = t_tbl ! (j,x)
      if k /= -1
        then do l1 <- readArray arr (i+1, k+1)
                writeArray arr (i,j) $ max l0 (l1 + 1)
        else writeArray arr (i,j) l0
  return arr

main = do
  s <- BS.getLine
  t <- BS.getLine
  -- BS.length s <= 3000, BS.length t <= 3000, BS.all isAsciiLower s, BS.all isAsciiLower t
  let table = lcsTable s t
      n = BS.length s
      reconstruct i | i >= n = ""
                    | table ! (i,0) > table ! (i+1,0) = BS.index s i : reconstruct (i+1)
                    | otherwise = reconstruct (i+1)
  putStrLn (reconstruct 0)