Submission #37417172


Source Code Expand

{-# LANGUAGE MultiParamTypeClasses #-}
module Main where

import qualified Data.ByteString.Char8 as B
import qualified Data.Map as M 
import Data.Maybe ( fromJust )

main :: IO ()
main = B.interact (encode . solve . decode)

decode :: B.ByteString -> [[String]]
decode =  map (map B.unpack . B.words) . B.lines

readInt :: B.ByteString -> Int
readInt = fst . fromJust . B.readInt

encode :: [[Int]] -> B.ByteString
encode = B.unlines . map (B.unwords . map showInt)

showInt :: Int -> B.ByteString
showInt = B.pack . show

solve :: [[String]] -> [[Int]]
solve dss = case dss of
    [s]:[t]:_ -> [[ length (lcs s t)] ]

lcs :: Eq a => [a] -> [a] -> [a]
lcs xs = fst . head . foldr phi row0
    where
        row0 = replicate (length xs + 1) nil
        phi y row = foldr (step y) [nil] (zip3 xs row (tail row))
        step y (x,cs1,cs2) row
            | x == y    = (x `cons` cs2) : row
            | otherwise = longer cs1 (head row) : row

longer :: Sized a -> Sized a -> Sized a
longer xs@(_,m) ys@(_,n)
    | m < n     = ys
    | otherwise = xs

type Sized a = ([a], Int)

nil :: Sized a 
nil = ([],0)

cons :: a -> Sized a -> Sized a
cons x (xs,n) = (x:xs, succ n)

Submission Info

Submission Time
Task A20 - LCS
User nobsun
Language Haskell (GHC 8.8.3)
Score 1000
Code Size 1172 Byte
Status AC
Exec Time 765 ms
Memory 224532 KiB

Compile Error

Loaded package environment from /home/contestant/.ghc/x86_64-linux-8.8.3/environments/default

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 2
AC × 23
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All in01.txt, in02.txt, in03.txt, in04.txt, in05.txt, in06.txt, in07.txt, in08.txt, in09.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in20.txt, in21.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
in01.txt AC 6 ms 3476 KiB
in02.txt AC 2 ms 3452 KiB
in03.txt AC 2 ms 3472 KiB
in04.txt AC 2 ms 3468 KiB
in05.txt AC 760 ms 219376 KiB
in06.txt AC 757 ms 224532 KiB
in07.txt AC 765 ms 221508 KiB
in08.txt AC 656 ms 119060 KiB
in09.txt AC 667 ms 117040 KiB
in10.txt AC 665 ms 119084 KiB
in11.txt AC 436 ms 144660 KiB
in12.txt AC 218 ms 75028 KiB
in13.txt AC 56 ms 19464 KiB
in14.txt AC 4 ms 4616 KiB
in15.txt AC 216 ms 46356 KiB
in16.txt AC 71 ms 14612 KiB
in17.txt AC 14 ms 5932 KiB
in18.txt AC 2 ms 3772 KiB
in19.txt AC 355 ms 134464 KiB
in20.txt AC 613 ms 157984 KiB
in21.txt AC 67 ms 15808 KiB
sample_01.txt AC 3 ms 3468 KiB
sample_02.txt AC 2 ms 3496 KiB