zoukankan      html  css  js  c++  java
  • 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)]
  • 相关阅读:
    缓存架构设计细节二三事
    啥,又要为表增加一列属性?
    SpringMvc4.x---快捷的ViewController
    SpringMvc4.x--@ControllerAdvice注解
    SpringMvc4.x--Spring MVC的常用注解
    解决svn--Unable to connect to a repository at URL ‘https://xxxxxx’ 问题
    或许你不知道的10条SQL技巧
    Java 基础-运算符
    Java 运算符 % 和 /
    Java基础-注释
  • 原文地址:https://www.cnblogs.com/sherylwang/p/5528604.html
Copyright © 2011-2022 走看看