思路:
dp。
实现:
1 class Solution 2 { 3 public: 4 bool judge(vector<string>& wordDict, string s) 5 { 6 return find(wordDict.begin(), wordDict.end(), s) != wordDict.end(); 7 } 8 bool wordBreak(string s, vector<string>& wordDict) 9 { 10 vector<bool> dp(s.length() + 1, 0); 11 dp[0] = true; 12 for (int i = 1; i <= (int)s.length(); i++) 13 { 14 for (int j = 0; j < i; j++) 15 { 16 if (dp[j] && judge(wordDict, s.substr(j, i - j))) 17 { 18 dp[i] = true; 19 } 20 } 21 } 22 return dp[s.length()]; 23 } 24 };