Dynamic programming - Algorithm to repair text where is all punctuation missing

二次信任 提交于 2019-12-02 12:25:51

The dynamic programming approach could be based on the following recursive formula:

f(n) = true
f(i) =  OR { d(s[i]s[i+1]...s[j-1]) AND f(j) | for each j such that i < j <= n }

Start from f(0).

Explanation:
If you got to the end of the string - you're good (base clause).
Otherwise, try to split the word wherever you can from the given start (which is i), and recursively invoke on the suffix of the string.

Complexity is actually O(n^2 * g(n)) where g(n) is the time it takes for dict to check if the word is in it. (Every solution need to depend on it somehow...)

Converting to DP solution:

Fill the table from last to first, according to the logic described in the recursive formula.

Simple solution in O(n^2) will be :-

DP(n) = (DP(0) && dict(S[1..n]) || (DP(1) && dict(S[2..n]))..... || (DP(n-1) && dict(S[n..n])
DP(0) = true

DP(n) = Whether there is valid sentence made from S[1...n]

S[i...j] is substring from ith index to jth index
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!