Word Break

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
        maxWordLen = 0
        for w in wordDict:
            maxWordLen = max(maxWordLen,len(w))
        res = [False] * (len(s)+1)
        res[0] = True 
        for i in xrange(1,len(s)+1):
            for j in xrange(i-1,max(i-maxWordLen-1,-1),-1):
                if res[j] == True and  (s[j:i] in wordDict):
                    res[i] = True
                    break
        return res[len(s)]

 

posted on 2016-05-25 21:38  Sheryl Wang  阅读(183)  评论(0编辑  收藏  举报

导航