Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
分析题目,是给定的一个字符串,是否能用已有的词表完成分词切割。
用DP的方法来做。
首先定义 dp[i] 表示 s[0, i] 是否能完成分词(可以完全分为词表中存在的词)。故 dp[0] 表示 s[0, 0],即空串。
1,初始状态:dp[0] = true
2,递推式:i由1到len遍历,将子字符串 s[0, i] 分为 s[0, j] 与 [j, i],前串是否已经能分词由 dp[j] 判断。若前段已经不可分,则后段怎么可分也无用。前段可分时,再看后串是否能匹配到词表即可,若能匹配到,则整个子串[0, i]可以分词,此时break即可。
bool wordBreak(string s, vector<string>& wordDict) {
if(wordDict.size()==0) return false;
int len = s.size();
int dict_size = wordDict.size();
// dp[i]表示字符串 s[0,i] (即0~i-1位) 是否能完成分词。
// 故dp[0]表示空串,dp长度应为len+1,因为 s[0,len](0~len-1位)应为dp[len+1]来判断
vector<bool> dp(len+1, false);
dp[0] = true;
// 循环遍历,i从 1到len,已知i处即为字符串的i-1处
for(int i=1; i<=len ; i++) {
// j 从 i-1到0,即j将子字符串[0, i] 分为[0, j] 与 [j, i]
for(int j=i-1; j>=0; j--) {
if (dp[j]) { // 前串[0, j]可分
string subword = s.substr(j, i-j);
// 若后段存在于字典中
if(find(wordDict.begin(), wordDict.end(), subword) != wordDict.end()){
dp[i] = true;
break;
}
}
}
}
return dp[len];
}