php 分词 动态规划,为什么分词需要动态规划?

我指的是这个问题:Given a dictionary, i.e., a set of strings, and a string s, design an

efficient algorithm that checks whether s is the concatenation of a

sequence of dictionary words. If such a concatenation exists, your

algorithm should output it.

以下是我在不使用DP的情况下解决问题的方法:def getwords(s, start = 0):

# Find a valid word as a prefix, and try to made the rest work

for i in range(start + 1, len(s) + 1):

prefix = s[start:i]

if isind(prefix):

# We used the whole thing, but it's a word!

if i == len(s):

return [prefix]

words = getwords(s, i)

if words:

return [prefix] + words

# We made it to the end without finding a word configuration

return False

DP算法被记录在here,并在“编程元素访谈”一书中。我的问题是:为什么?

我找不到任何非DP解决方案重新计算相同子问题的实例。有人能解释为什么这个算法不如DP算法吗?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值