forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_dp_n3.cpp
58 lines (48 loc) · 1.29 KB
/
AC_dp_n3.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dp_n3.cpp
* Create Date: 2015-04-15 20:27:12
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int len = s.length();
vector<vector<int> > dp(len + 1, vector<int>(len + 1));
for (int slen = 1; slen <= len; ++slen) {
for (int start = 0; start + slen <= len; ++start) {
int end = start + slen;
// find in dict
if (wordDict.find(s.substr(start, slen)) != wordDict.end()) {
dp[start][end] = true;
continue;
}
// find in sub-string
for (int i = start + 1; i < end; ++i) {
if (dp[start][i] && dp[i][end]) {
dp[start][end] = true;
break;
}
}
}
}
return dp[0][len];
}
};
int main() {
Solution s;
string a, b;
int n;
unordered_set<string> wordDict;
cin >> a;
cin >> n;
while (n--) {
cin >> b;
wordDict.insert(b);
}
cout << s.wordBreak(a, wordDict) << endl;
return 0;
}