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 ViewPatterns #-}
import qualified Data.Map as M
import qualified Data.ByteString.Char8 as BS

type DP = M.Map (Int,Int) Int

solve :: BS.ByteString -> BS.ByteString -> (Int,Int) -> DP -> DP
solve _ (BS.uncons -> Nothing) _ dp = dp
solve ss (BS.uncons -> Just (t, ts)) point dp =
  loop ss t point dp
  where
    loop :: BS.ByteString -> Char -> (Int,Int) -> DP -> DP
    loop (BS.uncons -> Nothing) _ (y,x) dp = solve ss ts (y+1,1) dp
    loop (BS.uncons -> Just (a, as)) b (y,x) dp
      | a == b = 
        case M.lookup (y-1,x-1) dp of
          Just v -> loop as b (y,x+1) $ M.insert (y,x) (v+1) dp
          Nothing -> loop as b (y,x+1) $ M.insert (y,x) 1 dp
      | otherwise = 
        let f = findFromPair (y-1,x) (y,x-1) dp in loop as b (y,x+1) $ M.insert (y,x) f dp
    
    findFromPair :: (Int,Int) -> (Int,Int) -> DP -> Int
    findFromPair p q dp =
      case [(M.lookup p dp), (M.lookup q dp)] of
        [Nothing, Nothing] -> 0
        [Just v, Nothing] -> v
        [Nothing, Just w] -> w
        [Just v, Just w] -> if v >= w then v else w

main = do
  s <- BS.getLine
  t <- BS.getLine
  let dp = solve s t (1,1) M.empty
  print $ M.deleteFindMax dp