Commit 325938fb authored by Tanmay Khanna's avatar Tanmay Khanna
Browse files

M3M35

parent c38156ab
import Data.List
data SuffixTree = Leaf Int | Node [(String, SuffixTree)]
deriving (Eq, Show)
------------------------------------------------------
isPrefix :: String -> String -> Bool
isPrefix pfix str
= pfix == take (length pfix) str
removePrefix :: String -> String -> String
--Pre: s is a prefix of s'
removePrefix
= drop . length
suffixes :: [a] -> [[a]]
suffixes str
= take (length str) (iterate tail str)
isSubstring :: String -> String -> Bool
isSubstring sub
= (any $ isPrefix sub) . suffixes
findSubstrings :: String -> String -> [Int]
findSubstrings sub str
= map fst $ filter (isPrefix sub . snd) (zip [0..] (suffixes str))
------------------------------------------------------
getIndices :: SuffixTree -> [Int]
getIndices (Leaf n)
= [n]
getIndices (Node ts)
= concatMap (getIndices . snd) ts
partitions :: Eq a => [a] -> [a] -> ([a], [a], [a])
partitions str1 str2
= partition' ([], str1, str2)
where
partition' :: Eq a => ([a], [a], [a]) -> ([a], [a], [a])
partition' (pfix, (s : str1), (s' : str2))
| s == s' = partition' ((s : pfix), str1, str2)
| otherwise = (reverse pfix, (s : str1), (s' : str2))
partition' (pfix, str1, str2)
= (pfix, str1, str2)
findSubstrings' :: String -> SuffixTree -> [Int]
findSubstrings' [] (Leaf n)
= [n]
findSubstrings' str (Leaf n)
= []
findSubstrings' str (Node [])
= []
findSubstrings' str (Node ((s, st) : ts))
| null str' = getIndices st
| null sub = findSubstrings' str' st
| otherwise = findSubstrings' str (Node ts)
where
(p, str', sub) = partitions str s
------------------------------------------------------
insert' :: (String, Int) -> SuffixTree -> SuffixTree
insert' (str, n) (Node [])
= Node [(str, Leaf n)]
insert' (str, n) (Node ((s, st) : ts))
| null p = Node ((s, st) : ts')
| p == s = Node ((s, insert' (str', n) st) : ts)
| otherwise = Node ((p, Node [(str', Leaf n), (s', st)]) : ts)
where
(p, str', s') = partitions str s
Node ts' = insert' (str, n) (Node ts)
-- This function is given
buildTree :: String -> SuffixTree
buildTree s
= foldl (flip insert') (Node []) (zip (suffixes s) [0..length s-1])
------------------------------------------------------
-- Part IV
longestRepeatedSubstring :: SuffixTree -> String
longestRepeatedSubstring
= undefined
------------------------------------------------------
-- Example strings and suffix trees...
s1 :: String
s1
= "banana"
s2 :: String
s2
= "mississippi"
t1 :: SuffixTree
t1
= Node [("banana", Leaf 0),
("a", Node [("na", Node [("na", Leaf 1),
("", Leaf 3)]),
("", Leaf 5)]),
("na", Node [("na", Leaf 2),
("", Leaf 4)])]
t2 :: SuffixTree
t2
= Node [("mississippi", Leaf 0),
("i", Node [("ssi", Node [("ssippi", Leaf 1),
("ppi", Leaf 4)]),
("ppi", Leaf 7),
("", Leaf 10)]),
("s", Node [("si", Node [("ssippi", Leaf 2),
("ppi", Leaf 5)]),
("i", Node [("ssippi", Leaf 3),
("ppi", Leaf 6)])]),
("p", Node [("pi", Leaf 8),
("i", Leaf 9)])]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment