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)

import           Control.Monad
import           Data.Array.ST
import           Data.Array.Unboxed    (UArray)
import qualified Data.Array.Unboxed    as U
import           Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS
import qualified Data.Vector           as V
import qualified Data.Vector.Unboxed   as VU

main :: IO ()
main = do
  s <- BS.getLine
  t <- BS.getLine
  let solve = solve2
  putStrLn $ solve s t

table2 :: ByteString -> ByteString -> V.Vector (VU.Vector Int)
table2 xs ys = V.scanl step (VU.replicate (m+1) 0) xs'
  where n = BS.length xs
        m = BS.length ys
        xs' = V.generate n $ BS.index xs
        ys' = VU.generate m $ BS.index ys
        -- <(i-1,j)> -> i -> <(i,j)>
        step :: VU.Vector Int -> Char -> VU.Vector Int
        step v x = VU.scanl innerStep 0 $ VU.zip3 v (VU.tail v) ys'
          where
            -- (i,j-1) -> ((i-1,j-1),(i-1,j),j) -> (i,j)
            -- 第二引数はvから生成されているので、i-1 (iではなく)
            innerStep :: Int -> (Int,Int,Char) -> Int
            innerStep a (b,c,y) | x == y = b + 1
                                | otherwise = max a c

solve2 :: ByteString -> ByteString -> String
solve2 s t = go (BS.length s) (BS.length t) ""
  where tbl = table2 s t
        go 0 _ ss = ss
        go _ 0 ss = ss
        go i j ss
          | tbl V.! i VU.! j == tbl V.! (i-1) VU.! j = go (i-1) j ss
          | tbl V.! i VU.! j == tbl V.! i VU.! (j-1) = go i (j-1) ss
          | otherwise                                = go (i-1) (j-1) ((s `BS.index` (i-1)):ss)