Suppose we just want to find one possible answer.
Recursion is a good method to use. LIKE this:
S = Corrupted String . replace(' ', '') // remove all [:space:] def recursive(position): if position == len(S): // A return True // we found a solution! for i in range(1, len(S) - position + 1): if S[position: poition + i] in Dictionary: // B if recursive(position + i): return True else: break // Find nothing, just return False return False recursive(0)
OK, now we can find the answer, BUT what is Big (O)?
In addition, we have two positions to accelerate:
but. when we find a word in a dictionary, we can use a trie-tree ( http://en.wikipedia.org/wiki/Trie ) to speed it up. Therefore, we avoid looking for it from the Red-Black Tree every time
V. we must remember the calculated answer: dynamic programming is a good solution, since now you use the recursive method, you can use dynamic programming memoization [ What is the difference between memoization and dynamic programming?
Now complexity is O (n * k)
kaitian521
source share