C++ 代码
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> f(s.size() + 1, false);
f[0] = true;
for (int i = 1; i <= s.size(); i++)
{
for (int j = 0; j < i; j++)
{
for (int k = 0; k < wordDict.size(); k++)
{
string t = s.substr(j, i-j);
if (f[j] && wordDict[k] == t)
f[i] = true;
t.clear();
}
}
}
return f[s.size()];
}
};