C++實(shí)現(xiàn)LeetCode(140.拆分詞句之二)
[LeetCode] 140.Word Break II 拆分詞句之二
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.
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 = "catsanddog" wordDict = ["cat", "cats", "and", "sand", "dog"] Output: [ "cats and dog", "cat sand dog" ]
Example 2:
Input:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
Output:
[
"pine apple pen apple",
"pineapple pen apple",
"pine applepen apple"
]
Explanation: Note that you are allowed to reuse a dictionary word.
Example 3:
Input:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
Output:
[]
這道題是之前那道Word Break 拆分詞句的拓展,那道題只讓我們判斷給定的字符串能否被拆分成字典中的詞,而這道題加大了難度,讓我們求出所有可以拆分成的情況,就像題目中給的例子所示。之前的版本中字典wordDict的數(shù)據(jù)類型是HashSet,現(xiàn)在的不知為何改成了數(shù)組vector,而且博主看到第二個(gè)例子就笑了,PPAP么,哈哈~
根據(jù)老夫行走江湖多年的經(jīng)驗(yàn),像這種返回結(jié)果要列舉所有情況的題,十有八九都是要用遞歸來做的。當(dāng)我們一時(shí)半會(huì)沒有啥思路的時(shí)候,先不要考慮代碼如何實(shí)現(xiàn),如果就給你一個(gè)s和wordDict,不看Output的內(nèi)容,你會(huì)怎么找出結(jié)果。比如對(duì)于例子1,博主可能會(huì)先掃一遍wordDict數(shù)組,看有沒有單詞可以當(dāng)s的開頭,那么我們可以發(fā)現(xiàn)cat和cats都可以,比如我們先選了cat,那么此時(shí)s就變成了 "sanddog",我們?cè)僭跀?shù)組里找單詞,發(fā)現(xiàn)了sand可以,最后剩一個(gè)dog,也在數(shù)組中,于是一個(gè)結(jié)果就出來了。然后回到開頭選cats的話,那么此時(shí)s就變成了 "anddog",我們?cè)僭跀?shù)組里找單詞,發(fā)現(xiàn)了and可以,最后剩一個(gè)dog,也在數(shù)組中,于是另一個(gè)結(jié)果也就出來了。那么這個(gè)查詢的方法很適合用遞歸來實(shí)現(xiàn),因?yàn)閟改變后,查詢的機(jī)制并不變,很適合調(diào)用遞歸函數(shù)。再者,我們要明確的是,如果不用記憶數(shù)組做減少重復(fù)計(jì)算的優(yōu)化,那么遞歸方法跟brute force沒什么區(qū)別,大概率無法通過OJ。所以我們要避免重復(fù)計(jì)算,如何避免呢,還是看上面的分析,如果當(dāng)s變成 "sanddog"的時(shí)候,那么此時(shí)我們知道其可以拆分成sand和dog,當(dāng)某個(gè)時(shí)候如果我們又遇到了這個(gè) "sanddog"的時(shí)候,我們難道還需要再調(diào)用遞歸算一遍嗎,當(dāng)然不希望啦,所以我們要將這個(gè)中間結(jié)果保存起來,由于我們必須要同時(shí)保存s和其所有的拆分的字符串,那么可以使用一個(gè)HashMap,來建立二者之間的映射,那么在遞歸函數(shù)中,我們首先檢測當(dāng)前s是否已經(jīng)有映射,有的話直接返回即可,如果s為空了,我們?nèi)绾翁幚砟?,題目中說了給定的s不會(huì)為空,但是我們遞歸函數(shù)處理時(shí)s是會(huì)變空的,這時(shí)候我們是直接返回空集嗎,這里有個(gè)小trick,我們其實(shí)放一個(gè)空字符串返回,為啥要這么做呢?我們觀察題目中的Output,發(fā)現(xiàn)單詞之間是有空格,而最后一個(gè)單詞后面沒有空格,所以這個(gè)空字符串就起到了標(biāo)記當(dāng)前單詞是最后一個(gè),那么我們就不要再加空格了。接著往下看,我們遍歷wordDict數(shù)組,如果某個(gè)單詞是s字符串中的開頭單詞的話,我們對(duì)后面部分調(diào)用遞歸函數(shù),將結(jié)果保存到rem中,然后遍歷里面的所有字符串,和當(dāng)前的單詞拼接起來,這里就用到了我們前面說的trick。for循環(huán)結(jié)束后,記得返回結(jié)果res之前建立其和s之間的映射,方便下次使用,參見代碼如下:
解法一:
class Solution { public: vector<string> wordBreak(string s, vector<string>& wordDict) { unordered_map<string, vector<string>> m; return helper(s, wordDict, m); } vector<string> helper(string s, vector<string>& wordDict, unordered_map<string, vector<string>>& m) { if (m.count(s)) return m[s]; if (s.empty()) return {""}; vector<string> res; for (string word : wordDict) { if (s.substr(0, word.size()) != word) continue; vector<string> rem = helper(s.substr(word.size()), wordDict, m); for (string str : rem) { res.push_back(word + (str.empty() ? "" : " ") + str); } } return m[s] = res; } };
我們也可以將將主函數(shù)本身當(dāng)作遞歸函數(shù),這樣就不用單獨(dú)的使用一個(gè)遞歸函數(shù)了,不過我們的HashMap必須是全局了,寫在外部就好了,參見代碼如下:
解法二:
class Solution { public: unordered_map<string, vector<string>> m; vector<string> wordBreak(string s, vector<string>& wordDict) { if (m.count(s)) return m[s]; if (s.empty()) return {""}; vector<string> res; for (string word : wordDict) { if (s.substr(0, word.size()) != word) continue; vector<string> rem = wordBreak(s.substr(word.size()), wordDict); for (string str : rem) { res.push_back(word + (str.empty() ? "" : " ") + str); } } return m[s] = res; } };
類似題目:
參考資料:
https://leetcode.com/problems/word-break-ii/description/
https://leetcode.com/problems/word-break-ii/solution/
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(140.拆分詞句之二)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)拆分詞句之二內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(152.求最大子數(shù)組乘積)
- C++實(shí)現(xiàn)LeetCode(151.翻轉(zhuǎn)字符串中的單詞)
- C++實(shí)現(xiàn)LeetCode(150.計(jì)算逆波蘭表達(dá)式)
- C++實(shí)現(xiàn)LeetCode(149.共線點(diǎn)個(gè)數(shù))
- C++實(shí)現(xiàn)LeetCode(148.鏈表排序)
- C++實(shí)現(xiàn)LeetCode(147.鏈表插入排序)
- C++實(shí)現(xiàn)LeetCode(146.近最少使用頁面置換緩存器)
- C++實(shí)現(xiàn)LeetCode(153.尋找旋轉(zhuǎn)有序數(shù)組的最小值)
相關(guān)文章
MFC擴(kuò)展DLL中導(dǎo)出類和對(duì)話框的實(shí)現(xiàn)方法
這篇文章主要介紹了MFC擴(kuò)展DLL中導(dǎo)出類和對(duì)話框的實(shí)現(xiàn)方法,詳細(xì)講述了實(shí)現(xiàn)擴(kuò)展DLL中導(dǎo)出類和對(duì)話框的具體步驟與方法,具有不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10詳解c/c++賦值函數(shù)(重載=號(hào)運(yùn)算符)
大家都知道c++里的各種運(yùn)算符都是用函數(shù)實(shí)現(xiàn)的,比如=就等號(hào)函數(shù),所以當(dāng)用=給一個(gè)對(duì)象賦值的時(shí)候,實(shí)際調(diào)用的是=號(hào)所對(duì)應(yīng)的=號(hào)函數(shù)。下面通過本文給大家介紹c/c++賦值函數(shù)(重載=號(hào)運(yùn)算符),感興趣的朋友一起看看吧2018-08-08舉例講解C語言程序中對(duì)二叉樹數(shù)據(jù)結(jié)構(gòu)的各種遍歷方式
這篇文章主要介紹了舉例講解C語言程序中對(duì)二叉樹數(shù)據(jù)結(jié)構(gòu)的各種遍歷方式,先序中序后序二叉樹遍歷幾乎成了最老生常談的數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)知識(shí),的朋友可以參考下2016-04-04OpenCV reshape函數(shù)實(shí)現(xiàn)矩陣元素序列化
reshape函數(shù)是OpenCV中一個(gè)很有用的函數(shù),不僅可以改變矩陣的通道數(shù),還可以對(duì)矩陣元素進(jìn)行序列化。本文將主要介紹如何通過reshape實(shí)現(xiàn)矩陣元素序列化,需要的小伙伴可以參考一下2021-12-12