码迷,mamicode.com
首页 > 其他好文 > 详细

Word Break

时间:2016-05-25 22:11:41      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet", "code"]. Return true because "leetcode" can be segmented as "leet code".

这是一道和DP算法导论上锯钢条的题目十分类似的题目。属于单序列动态规划。

1.首先定义状态,f[i]表示前i个字符能不能被分词。

2.定义转化状态 f[i] = OR(f[j] && j+1~i 是单词)

3.初始化和方向f[0]=0,从前往后走。

4.解是f[len(s)]

看到转化状态是OR的就想到这种需要去除冗余,及时break,因为是判断单词,所以从后超找j比较方便。

代码如下:

class Solution(object):
    def wordBreak(self, s, wordDict):
        """
        :type s: str
        :type wordDict: Set[str]
        :rtype: bool
        """
        if not s:
            return True 
        if not wordDict:
            return False
        
        res = [False] * (len(s)+1)
        res[0] = True 
        for i in xrange(1,len(s)+1):
            for j in xrange(i-1,-1,-1):
                if res[j] == True and  (s[j:i] in wordDict):
                    res[i] = True
                    break
        return res[len(s)]

上述解法不排除在匹配不上单词时一直遍历所有字符串,在Lintcode上超时。可以做一个优化,即先获得词典中单词的最长长度,这样超前找单词时,达到最长单词长度时没有找到就停止寻找,代码如下:

class Solution(object):
    def wordBreak(self, s, wordDict):
        """
        :type s: str
        :type wordDict: Set[str]
        :rtype: bool
        """
        if not s:
            return True 
        if not wordDict:
            return False
        
        res = [False] * (len(s)+1)
        res[0] = True 
        for i in xrange(1,len(s)+1):
            for j in xrange(i-1,-1,-1):
                if res[j] == True and  (s[j:i] in wordDict):
                    res[i] = True
                    break
        return res[len(s)]

 

Word Break

标签:

原文地址:http://www.cnblogs.com/sherylwang/p/5528604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!