标签:
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"
.
#include<iostream> #include<vector> #include<unordered_set> using namespace std; bool IsWord(string s, unordered_set<string> &dict) { unordered_set<string>::iterator Iter = dict.find(s); if (Iter == dict.end()) return false; else return true; } bool wordBreak(string s, unordered_set<string> &dict) { vector<bool>FalgWord(s.size()+1,false); FalgWord[0] = true; for (int i = 0; i <= s.size();++i) { for (int j = 0; j != i;++j) { if (FalgWord[i]) break; else FalgWord[i] = FalgWord[j] && IsWord(s.substr(j, i - j), dict); } } return FalgWord[s.size()]; }
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"
.
标签:
原文地址:http://blog.csdn.net/li_chihang/article/details/43532823