-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path140.cpp
More file actions
71 lines (65 loc) · 1.94 KB
/
140.cpp
File metadata and controls
71 lines (65 loc) · 1.94 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// 140.cpp
// LeetCode
//
// Created by 张佐玮 on 15/7/15.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Word Break II
//
#include <iostream>
#include <unordered_set>
#include <vector>
#include <string>
#include <stack>
using namespace std;
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
vector<vector<unordered_set<string>::iterator>> matched;
for (int i = 0; i < s.size(); i++) {
vector<unordered_set<string>::iterator> temp;
for (int j = 0; j <= i; j++) {
auto subStr = wordDict.find(s.substr(j, i-j+1));
if ((j == 0 || !matched[j-1].empty()) && subStr != wordDict.end()) {
temp.push_back(subStr);
}
}
matched.push_back(temp);
}
string current = "";
vector<string> result;
makeSentence(matched, current, result, matched.size()-1);
return result;
}
void makeSentence(vector<vector<unordered_set<string>::iterator>> &matched, string current, vector<string> &result, long loc)
{
if (loc < 0) {
if (!current.empty()) {
result.push_back(current.erase(current.size()-1));
}
return;
}
for (auto it : matched[loc]) {
makeSentence(matched, *it + " " + current, result, loc-(*it).size());
}
}
};
class Test {
private:
static void runTest(string s, string dict[], int length) {
unordered_set<string> wordDict(dict, dict+length);
Solution solution;
vector<string> result = solution.wordBreak(s, wordDict);
for (auto str : result) {
cout << str << endl;
}
}
public:
void sample() {
string s1("catsanddog");
string dict1[] = {"cat","cats","and","sand","dog"};
int length1 = 5;
runTest(s1, dict1, length1);
}
};