动态规划问题
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
int n = s.size(), ws = wordDict.size();
unordered_set<string> us(wordDict.begin(), wordDict.end());
vector<string>::iterator it;
bool *dp = new bool[n + 1];
fill(dp, dp + n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; ++i)
{
for (it = wordDict.begin(); it != wordDict.end(); ++it)
{
if (i >= (*it).size() && dp[i - (*it).size()] && s.substr(i - (*it).size(), (*it).size()) == *it)
{
dp[i] = 1;
}
}
}
return dp[n];
}
};