-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlintcode582Word Break II.cpp
More file actions
55 lines (53 loc) · 1.17 KB
/
Copy pathlintcode582Word Break II.cpp
File metadata and controls
55 lines (53 loc) · 1.17 KB
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
#include<vector>
#include<string>
#include<iostream>
#include<unordered_set>
using namespace std;
class Solution {
public:
/*
* @param s: A string
* @param wordDict: A set of words.
* @return: All possible sentences.
*/
// unordered<string> = {};
void dfs(vector<string> &res, string &tem, const string &s, unordered_set<string> &wordDict)
{
int n = s.size();
if(s.empty())
{
res.push_back(tem);
return;
}
for(int i = 1; i <= n; ++i)
{
if(wordDict.find(s.substr(0, i)) != wordDict.end())
{
int flag = 0;
if(!tem.empty()) tem +=" ", flag = 1;
tem += s.substr(0, i);
dfs(res, tem, s.substr(i), wordDict);
tem = tem.substr(0, tem.size() - flag - i);
}
}
}
vector<string> wordBreak(string &s, unordered_set<string> &wordDict) {
// write your code here
vector<string> res;
string tem;
dfs(res, tem, s, wordDict);
return res;
}
};
int main()
{
Solution solve;
unordered_set<string> wordDict = {"a", "b","c","ab", "bc", "d"};
string s = "aabc";
auto out = solve.wordBreak(s, wordDict);
for(auto i:out)
{
cout<<"res: "<<i<<endl;
}
// cout<<s.substr(0,0);
}