使用trie没有空格的字符串

时间:2017-06-26 10:50:27

标签: python algorithm dynamic-programming trie

我仍然很难理解算法中的所有步骤,我应该如何处理输入字符串的所有可能分区,而没有基于字典的空格。

我读到我们必须使用TRIE结构使用一些动态编程和反向跟踪(深度优先搜索),但我从概念上不了解它是如何工作的。

首先我想要的是:

input 
string = `theology`

    dictionnary 
    dict = [the , theology]

output i am expecting

[the, o, l, o, g, y ] ,[theology]

关于我“这里的错误理解是我看到的步骤”(我使用R语言,但我更需要解释逻辑而不是特定的语言答案)

0 - Transform my dictionnary into a trie 

```
library("triebeard")
trie <- trie(keys = c("the", "theology","toto","what"),
             values = c("the", "theology",
             "toto","what"))
```

1 - calculate the number of character on the string 
    - On this case I have 8.

2 - Take character by character n+1 and look inside the trie

2.1 I take the first character is t so i look  at t and do the longest match 
    It return NA so i go to the next character
    ```
    longest_match(trie, "t")
    ```
    2.2 Next char `"th"` returns NA 

2.3 Next char "the" returns `"the"` so i add it the a list 

3a - Now i don't know what to do should I continue to the 4 position take one character
look at the longest match see NA and so on so forth 

3b - Or should go and look at  `"theo"`

我猜这与前缀匹配有关,而不是与最长的匹配。

如果您可以指导我如何使用trie获得预期的输出

[更新]

为了更好地理解我的问题,这里有一个可能的解决方案在python使用字典,我发现/修改了感谢interjay的回答:https://stackoverflow.com/a/2174156/5348895

wordList = ['the','theology'] words = set( s.lower() for s in wordList ) def splitString(s): found = [] def rec(stringLeft, wordsSoFar): if not stringLeft: found.append(wordsSoFar) for pos in xrange(1, len(stringLeft)+1): if stringLeft[:pos] in words: rec(stringLeft[pos:], wordsSoFar + [stringLeft[:pos]]) elif (pos==len(stringLeft)) and stringLeft[:pos] not in wordList and len(stringLeft)<len(s): rec(stringLeft[1:], wordsSoFar + [stringLeft[:1]]) rec(s.lower(), []) return found output = splitString('theology') for x in output: print ','.join(x)

由于

0 个答案:

没有答案