要求:字符串是否能拆分为字典里单词
思路:单词可重复,暗示这是一道完全背包,然后本题只求布尔值两种循环都行?错,applepenapple,apple重复出现,所以本题必须把物品放里面
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
vector<bool> dp(s.length()+1);
dp[0]=true;
for(int j=1;j<=s.length();++j){
for(auto& w:wordDict){
if(j>=w.length()&&w==s.substr(j-w.length(),w.length()))
dp[j]=dp[j]||dp[j-w.length()];
}
}
return dp[s.length()];
}
};