[Leetcode] 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".
题意:S是否可以由dict中的字符串合成。
思路:动态规划。维护一个数组vector<bool> dp(s.size()+1,false),其中dp[i] 代表S中[0,i-1]是否用dict中的字符串表示,能,true,不能,false。对dp[i]而言,若dp[j] (j<i)能在dict中找到,则只需看s.substr(j,i-j)是否能在dict中找到,若能,则i++,重新分析,不能j++。这里值得注意的是:下标的问题。dp[j]表示S中s.substr(0,j)(前闭后开,所以代表S中[0, j-1] 子字符串 )能否在dict中找到。代码如下:
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict)
{
int len=s.size();
vector<bool> dp(len+,false);
dp[]=true; /
for(int i=;i<len+;++i)
{
for(int j=;j<i;++j)
{
if(dp[j]&&dict.find(s.substr(j,i-j)) !=dict.end())
{
dp[i]=true;
break;
}
}
}
return res[len];
}
};
网友Code Gander总结了动态规划的一般套路。
个人总结:
动态规划:基于一个递推公式以及一个或者多个初始状态。较为重要是:状态和状态转移方程!
三步曲:
一、存储什么历史信息以及用什么结构;
二、递推方程(重要);
三、起始条件;
最重要的还是知道,什么情况下用动态规划。
[Leetcode] word break 拆分词语的更多相关文章
- [LeetCode] Word Break 拆分词句
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- 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 ...
- LeetCode ||& Word Break && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
随机推荐
- collections模块的使用
1. Counter counter是collections中的一个模块, 它能够统计出字符串/文本中的每一个元素出现的次数, 并可以对结果进行进一步的处理. 使用方法 传入: 字符串 默认返回: C ...
- 基于OMAPL:Linux3.3内核的编译
基于OMAPL:Linux3.3内核的编译 OMAPL对应3个版本的linux源代码,分别是:Linux-3.3.Linux-2.6.37.Linux2.6.33,这里的差距在于Linux2,缺少SY ...
- 腾讯招聘网数据爬取存入mongodb
#!/user/bin/env python3 # -*- coding: utf-8 -*- import requests from lxml import etree from math imp ...
- 初识python 文件读取 保存
上一章最后一题的答案:infors.sort(key=lambda x:x['age'])print(infors)--->[{'name': 'laowang', 'age': 23}, {' ...
- C++ 求阶乘
#include<iostream> using namespace std; ; //输入阶乘数 int main() { long long factorial[size]; fact ...
- System.Speech使用
使用微软语音库 使用微软语音库可以很快速的制作一个小应用,比如一个唐诗的朗诵工具.本示例也是使用微软语音库,制作了一个唐诗宋词朗诵的应用,仅供加深学习印象 首先是要引入System.Speech库 然 ...
- [Jmeter]jmeter数据库性能测试配置
学习jmeter过程中,记录一些学习过程中的点点滴滴,用于备忘.本文主要介绍的是如何创建一个简单的测试计划用户测试数据库服务器. 一.添加线程组 二.添加JDBC请求 1.在第一步里面定义并发用户以及 ...
- P1095 守望者的逃离
P1095 守望者的逃离 题目描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这 ...
- LeetCode:12. Integer to Roman(Medium)
1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...
- goroutine 并发之搜索文件内容
golang并发编程 - 例子解析 February 26, 2013 最近在看<Programming in Go>, 其中关于并发编程写得很不错, 受益非浅, 其中有一些例子是需要多思 ...