Ich bin fest mit meinen Hausaufgaben Aufgabe, jemand helfen, bitte ..Strings aus Matrix
Hier ist die Aufgabe: Suche alle möglichen Partitionen der Zeichenfolge in Worte einiger Wörterbuch
Und hier ist wie ich versuche, es zu tun: ich dynamische Programmierkonzept verwenden Matrix zu füllen und dann bin ich stecken mit, wie zum abrufen von Daten aus es
-- 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)
Was genau meinen Sie mit "Finden Sie alle möglichen Partitionen der Zeichenfolge in Wörter eines Wörterbuchs"? Können Sie ein Beispiel zur Klärung des Problems geben? –
Wörterbuch = ["l", "la", "a"], string = "lala", Ergebnis = [["l", "a", "l", "a"], ["la", "" l, a, l, la, l, a, la. Ist das jetzt klar? – overwriter