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”.

记得最開始做动态规划的题时是打开过这道题的,可是那时没什么思路。如今动态规划的题刷了不少了,今天再做这道题时非常快就想出了状态和状态转移方程。不能不说还是有点进步。

定义A[i]表示0到下标为i的子字符是否能被切割成dict中的多个单词。

那么A[i]与A[j],0<=j< i都有关系,即A[i]与前A[]中的前i-1项都有关系,详细为:

  1. 假设A[0]为1。推断s中下标从1開始到i结束的字符是否在dict中,假设在,设置A[i]为1,跳出。否则进入第二步。
  2. 假设A[1]为1,推断s中下标从2開始到i结束的字符是否在dict中。假设在。设置A[i]为1,跳出,否则进入第二步;

    …..

    这样一直遍历到A[i-1]位置。

    在上面的遍历过程中假设遍历到某一步j,A[j]=1而且j+1到i表示的字符串出如今dict中,表示前j个字符串能切割成dict中的单词,j+1到i中的字符串串也能切割成dict中的单词。这样表示前i个字符能被切割成dict中的单词。

实际编写代码时,j能够从i開始倒着開始遍历,这样能够降低遍历的次数。

runtime:4ms

class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int length=s.size();
int *A=new int[length]();
for(int i=0;i<length;i++)
{
for(int j=i;j>=0;j--)
{
if(j==i)
{
A[i]=isExist(s,0,i,wordDict);
}
else if(A[j]==1)
{
A[i]=isExist(s,j+1,i,wordDict);
}
if(A[i]==1)
break;
}
} return A[length-1]==1; } int isExist(string &s,int first,int last,unordered_set<string> &wordDict)
{
string str=s.substr(first,last-first+1);
if(wordDict.count(str))
return 1;
else
return 0;
} };

LeetCode139:Word Break的更多相关文章

  1. Leetcode139. Word Break单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复 ...

  2. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  3. 【leetcode】Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  4. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  5. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  6. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  7. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  8. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  9. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

随机推荐

  1. WebGL学习(1) - 三角形

    原文地址:WebGL学习(1) - 三角形 还记得第一次看到canvas的粒子特效的时候,真的把我给惊艳到了,原来在浏览器也能做出这么棒的效果.结合<HTML5 Canvas核心技术>和网 ...

  2. T-SQL 删除重复数据SQL

    WITH cte AS (   SELECT roleid,permissionid,      row_number() OVER(PARTITION BY roleid,permissionid ...

  3. Virtual

    Virtual 作用: 允许在派生类中重新定义与基类同名函数并且可以通过其类的指针或引用来访问基类何派生类的同名函数. 1. 概述简单地说,每一个含有虚函数(无论是其本身的,还是继承而来的)的类都至少 ...

  4. 一个简单的基于BIO的RPC框架

    github地址:https://github.com/Luyu05/BioRpcExample PART1:先来整体看下项目的构成 其中bio-rpc-core就是所谓的rpc框架 bio-rpc- ...

  5. oracle数据库热备中的备份和恢复及例子

    手工热备(开库状态) 备份控制文件: alter database backup controlfile to '/u01/oradata/prod/con.bak1'; 备份数据文件(这里用到pl/ ...

  6. elastic-search单机部署以及中文分词IKAnalyzer安装

    前提条件 elasticsearch使用版本5.6.3,需要jdk版本1.8,低于该版本不能使用 下载 https://artifacts.elastic.co/downloads/elasticse ...

  7. hiero_v2.0的下载安装和使用

    程序地址:http://www.n4te.com/hiero/hiero.jnlp http://slick.cokeandcode.com/demos/hiero.jnlp(目测该网址需翻*墙才能进 ...

  8. C#中一些默认的预定义属性

    C#中一些默认的预定义属性,见下表: 预定义的属性 有效目标 说明 AttributeUsage Class 指定另一个属性类的有效使用方式 CLSCompliant 全部 指出程序元素是否与CLS兼 ...

  9. Android内存泄露的原因

    (一)释放对象的引用,误将一个本来生命周期短的对象存放到一个生命周期相对较长的对象中,也称“对象游离“.隐蔽的内部类(Anonymous Inner Class): mHandler = new Ha ...

  10. linux配置远程Git仓库

    一,安装git yum install git 二,在服务器(119.28.1.1)目录( /git/admin )上创建一个仓库 cd /git/admin touch aaa.html git i ...