// Source : https://oj.leetcode.com/problems/word-break/ // Author : Hao Chen // Date : 2014-07-01 /********************************************************************************** * * 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 #include #include using namespace std; bool wordBreak(string s, set &dict) { //using an array to mark subarray from 0 to i can be broken or not vector v(s.size(),false); for(int i=0; i dict; s = "a"; dict.insert("a"); cout << wordBreak(s, dict) << endl; dict.clear(); s = "dogs"; string d[]={"dog","s","gs"}; dict.insert(d, d+3); cout << wordBreak(s, dict) << endl; return 0; }