-
Notifications
You must be signed in to change notification settings - Fork 0
/
0139.cpp
58 lines (53 loc) · 1.34 KB
/
0139.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution
{
public:
/*
bool wordBreak(string s, vector<string>& wordDict)
{
int n = s.length();
vector<bool> dp(n + 1, false);
dp[0] = true;
for (int i = 1; i <= n; i++)
{
for (const string& word : wordDict)
{
int len = word.length();
if (i >= len && s.substr(i - len, len) == word) //在这里i看成是遍历到的长度
dp[i] = dp[i] || dp[i - len];
}
}
return dp[n];
}
*/
bool wordBreak(string s, vector<string>& wordDict)
{
int m = wordDict.size(), n = s.size();
vector<bool> dp(n + 1, false);
dp[0] = true;
for (int j = 1; j <= n; j++)
{
for (int i = 1; i <= m; i++)
{
int len = wordDict[i - 1].size();
string str = wordDict[i - 1];
if (j >= len && str == s.substr(j - len, len))
dp[j] = dp[j - len] || dp[j];
//cout << dp[j] << " ";
}
//cout << endl;
}
return dp[n];
}
};
int main()
{
string s = "leetcode";
vector<string> wordDict = { "leet", "code" };
Solution S;
S.wordBreak(s, wordDict);
return 0;
}