[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中找到。代码如下:

 1 class Solution {
 2 public:
 3     bool wordBreak(string s, unordered_set<string> &dict)
 4     {
 5         int len=s.size();
 6         vector<bool> dp(len+1,false);
 7         dp[0]=true;    /
 8
 9         for(int i=0;i<len+1;++i)
10         {
11             for(int j=0;j<i;++j)
12             {
13                 if(dp[j]&&dict.find(s.substr(j,i-j)) !=dict.end())
14                 {
15                     dp[i]=true;
16                     break;
17                 }
18             }
19         }
20         return res[len];
21     }
22 };

网友Code Gander总结了动态规划的一般套路。

个人总结:

动态规划:基于一个递推公式以及一个或者多个初始状态。较为重要是:状态和状态转移方程!

三步曲:

一、存储什么历史信息以及用什么结构;
二、递推方程(重要);

三、起始条件;

最重要的还是知道,什么情况下用动态规划。

时间: 2024-12-29 11:43:23

[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-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

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: Word Break [139]

[题目] 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 be

LeetCode: Word Break II [140]

[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cat

[leetcode]Word Break II @ Python

原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "c

[Leetcode] word break ii拆分词语

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens ="catsanddog",dict =["cat", "cats",

**LeetCode—Word Break

Given a string s and a dictionary of words dict, determine ifs 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 Word Break python

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, givens = "leetcode",dict = ["leet", "code"]. Return tru

Leetcode:Word Break 字符串分解为单词

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 t