【Leetcode】【DP】 139. Word Break/分詞
阿新 • • 發佈:2018-11-10
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];
}