-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlongest-common-prefix.hs
33 lines (28 loc) · 1.04 KB
/
longest-common-prefix.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{-
Prefix Compression (https://www.hackerrank.com/challenges/prefix-compression/problem)
FP/Recursion, Easy, 10 pts
You are in charge of data transfer between two Data Centers. Each set of data is
represented by a pair of strings. Over a period of time you have observed a
trend: most of the times both strings share some prefix. You want to utilize
this observation to design a data compression algorithm which will be used to
reduce amount of data to be transferred.
You are given two strings, x and y, representing the data, you need to find the
longest common prefix (p) of the two strings. Then you will send substring p, x'
and y', where x' and y' are the substrings left after stripping p from them.
-}
prefix :: String -> String -> String
prefix [] _ = []
prefix _ [] = []
prefix (a:as) (b:bs) = if a == b then a : prefix as bs else []
out :: String -> IO ()
out x = putStrLn $ show (length x) ++ " " ++ x
main :: IO ()
main = do
t <- getContents
let
[a,b] = lines t
p = prefix a b
pl = length p
out p
out (drop pl a)
out (drop pl b)