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 Control.Monad
import Control.Applicative
import qualified Data.Map as M
import Debug.Trace

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

solve :: String -> String -> (Int,Int) -> DP -> DP
solve _ [] _ dp = dp
solve ss (t:ts) point dp =
  loop ss t point dp
  where
    loop :: String -> Char -> (Int,Int) -> DP -> DP
    loop [] _ (y,x) dp = solve ss ts (y+1,1) dp
    loop (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) (a:v) dp
          Nothing -> loop as b (y,x+1) $ M.insert (y,x) [a] 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 -> String
    findFromPair p q dp =
      case [(M.lookup p dp), (M.lookup q dp)] of
        [Nothing, Nothing] -> undefined 
        [Just v, Nothing] -> v
        [Nothing, Just w] -> w
        [Just v, Just w] -> if length v >= length w then v else w
main = do
  s <- getLine
  t <- getLine
  let dp = solve s t (1,1) M.empty
  putStrLn $ reverse $ snd $ fst $ M.deleteFindMax dp