-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay30.cpp
69 lines (64 loc) · 1.37 KB
/
Day30.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
59
60
61
62
63
64
65
66
67
68
69
string s;int n;
unordered_map<string, int> mp;
bool cut(int start, int end, string a,vector<vector<char> >& dp)
{
if(start > end)
return 1;
if(dp[start][end] != '?')
{
if(dp[start][end] == '1')
return 1;
return 0;
}
string ans;
for(int i = start; i <= end; i++)
{
ans += a[i];
if(mp.find(ans) != mp.end())
if(cut(i+1, end, a, dp))
{
dp[start][end] = '1';
return 1;
}
}
dp[start][end] = '0';
return 0;
}
void backtrack(string s, vector<string> &ans, string yet, int idx)
{
if(idx == n)
{
yet.pop_back();
ans.push_back(yet);
return;
}
string temp = "";
for(int i = idx; i < n; i++)
{
temp += s[i];
if(mp.find(temp) != mp.end())
{
string ss = yet;
ss += temp;
ss += " ";
backtrack(s, ans, ss, i+1);
}
}
}
class Solution {
public:
vector<string> wordBreak(string A, vector<string>& B) {
vector<string> ans;
mp.clear();
s = A;
n = A.size();
for(int i = 0; i < B.size(); i++)
mp[B[i]] = 1;
vector<vector<char> > dp(n, vector<char>(n, '?'));
int x = cut(0, n-1, A, dp);
if(x == 0)
return ans;
backtrack(A, ans, "", 0);
return ans;
}
};