我堅持我的家庭作業任務,有人幫助,請檢索串..從矩陣
這裏的任務: 查找字符串的所有可能劃分成一些字典中的單詞
這裏是怎麼我試圖做到這一點: 我用動態規劃理念,以填補矩陣,然後我堅持瞭如何檢索數據從它
-- Task5_2
retrieve :: [[Int]] -> [String] -> Int -> Int -> Int -> [[String]]
retrieve matrix dict i j size
| i >= size || j >= size = []
| index /= 0 = [(dict !! index)]:(retrieve matrix dict (i + sizeOfWord) (i + sizeOfWord) size) ++ retrieve matrix dict i (next matrix i j) size
where index = (matrix !! i !! j) - 1; sizeOfWord = length (dict !! index)
next matrix i j
| j >= (length matrix) = j
| matrix !! i !! j > 0 = j
| otherwise = next matrix i (j + 1)
getPartitionMatrix :: String -> [String] -> [[Int]]
getPartitionMatrix text dict = [[ indiceOfWord (getWord text i j) dict 1 | j <- [1..(length text)]] | i <- [1..(length text)]]
--------------------------
getWord :: String -> Int -> Int -> String
getWord text from to = map fst $ filter (\a -> (snd a) >= from && (snd a) <= to) $ zip text [1..]
indiceOfWord :: String -> [String] -> Int -> Int
indiceOfWord _ [] _ = 0
indiceOfWord word (x:xs) n
| word == x = n
| otherwise = indiceOfWord word xs (n + 1)
-- TESTS
dictionary = ["la", "a", "laa", "l"]
string = "laa"
matr = getPartitionMatrix string dictionary
test = retrieve matr dictionary 0 0 (length string)
你是什麼意思的「查找所有可能的字符串分詞到某些字典的單詞」?你能舉一個例子來幫助澄清問題嗎? – 2012-03-30 13:07:19
dictionary = [「l」,「la」,「a」],string =「lala」,result = [[「l」,「a」,「l」,「a」], l「,」a「],[」la「,」la「],[」l「,」a「,」la「]。現在清楚了嗎? – overwriter 2012-03-30 13:29:26