题目:

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

思路:

1、DFS

2、动态规划

代码:

#include<iostream>
#include<unordered_set>
#include<vector> using namespace std; // dp
bool WordBreak_dp(string s,unordered_set<string> &wordDict){
int n=s.size(); vector<bool> dp(n+,false);
dp[]=true; for(int i=;i<n;i++){
for(int j=i;j>=;j--){
if(!dp[j]) continue;
if(wordDict.find(s.substr(j,i-j+))!=wordDict.end()){
dp[i+]=true;
break;
}
}
}
return dp[n]; } // dfs
bool WordBreak_dfs(string s,unordered_set<string> &wordDict){
if(s=="")
return true;
for(int i=;i<s.size();i++){
if(wordDict.find(s.substr(,i+))!=wordDict.end()){
if(WordBreak_dfs(s.substr(i+),wordDict))
return true;
}
}
return false;
} int main(){
unordered_set<string> wordDict;
wordDict.insert("leet");
wordDict.insert("code");
string s;
while(cin>>s){
cout<< WordBreak_dp(s,wordDict) <<endl;
cout<< WordBreak_dfs(s,wordDict) <<endl;
}
return ;
}

(算法)Word Break的更多相关文章

  1. LeetCode140:Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

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

  10. 140. Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

随机推荐

  1. 提高你的Java代码质量吧:谨慎包装类型的比较

    一.分析  基本类型可以比较大小,其所对应的包装类型都实现了Comparable接口此问题. 二.场景  代码如下: public class Client{ public static void m ...

  2. Redis源代码分析(三十五)--- redis.c服务端的实现分析(2)

    在Redis服务端的代码量真的是比較大,假设一个一个API的学习怎么实现,无疑是一种效率非常低的做法,所以我今天对服务端的实现代码的学习,重在他的运行流程上.而对于他的模块设计在上一篇中我已经分析过了 ...

  3. JavaScript进阶系列03,通过硬编码、工厂模式、构造函数创建JavaScript对象

    本篇体验通过硬编码.工厂模式.构造函数来创建JavaScript对象. □ 通过硬编码创建JavaScript对象 当需要创建一个JavaScript对象时,我们可能这样写: var person = ...

  4. C++笔记:头文件的作用和写法

    from://http://ceeji.net/blog/c%E7%AC%94%E8%AE%B0%EF%BC%9A%E5%A4%B4%E6%96%87%E4%BB%B6%E7%9A%84%E4%BD% ...

  5. 【转】IntelliJ IDEA关联SVN

    http://blog.csdn.net/xdd19910505/article/details/52756417 问题描述: IntelliJ IDEA安装之后,使用SVN进行提交或更新以及检出代码 ...

  6. Oracle的tnsnames.ora 监听配置文件详解

    监听配置文件 为了使得外部进程 如 CAMS后台程序 能够访问 Oracle 数据库 必须配 置 Oracle 网络服务器环境 配置 Oracle 网络服务器环境是通过配置 listener.ora ...

  7. Thinking in Java---异常处理机制

    java的异常处理机制能够使程序有极好的容错性,让程序更加的健壮.所谓的异常,就是指的阻止当前方法或作用域继续运行的问题,,当程序运行时出现异常时,系统就会自己主动生成一个Exception对象来通知 ...

  8. 用Eclipse给安卓应用进行签名

    Eclipse功能强大,用它来给应用进行签名也十分简单.下面是进行签名的步骤

  9. Orchard 前台权限与自定义权限

    一:关于前台权限 1:只允许自己看到 首先,我们需要确定在 Role 设置页面,用户所对应的 View Page by others 和 View all content 未被选中.备注,我们首先和得 ...

  10. SVG.js 颜色渐变使用

    一.SVG.Gradient 1.线性渐变.径向渐变,设置渐变的起始点,设置径向渐变的外层半径 var draw = SVG('svg1').size(300, 300); //SVG.Gradien ...