-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200324-1.cpp
129 lines (126 loc) · 2.86 KB
/
200324-1.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// https://leetcode-cn.com/problems/word-search-ii/
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
Trie trie;
for (auto& w : words) trie.Add(w);
unordered_set<string> m;
vector<string> res;
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); ++j) {
Search(board, i, j, trie, m, res, "");
}
}
return res;
}
private:
class Trie {
public:
Trie(): end(false) { }
~Trie() {
for (auto it = o.begin(); it != o.end(); ++it) {
delete it->second;
}
o.clear();
}
void Add(string s) {
if (s.empty()) {
end = true;
} else {
char c = s[0];
auto it = o.find(c);
Trie* p;
if (it == o.end()) {
p = new Trie();
o[c] = p;
} else {
p = it->second;
}
p->Add(s.substr(1));
}
}
bool Has(string s) const {
if (s.empty()) {
return end;
} else {
char c = s[0];
auto it = o.find(c);
if (it == o.end()) {
return false;
} else {
return it->second->Has(s.substr(1));
}
}
}
bool Match(string s) const {
if (s.empty()) {
return true;
} else {
char c = s[0];
auto it = o.find(c);
if (it == o.end()) {
return false;
} else {
return it->second->Match(s.substr(1));
}
}
}
private:
bool end;
unordered_map<char, Trie*> o;
};
void Search(vector<vector<char>>& board, int i, int j, const Trie& trie,
unordered_set<string>& m, vector<string>& res, string s) {
char c = board[i][j];
s += c;
if (!trie.Match(s)) return;
if (trie.Has(s) && m.find(s) == m.end()) {
m.insert(s);
res.push_back(s);
}
board[i][j] = '\0';
if (i > 0 && board[i - 1][j]) Search(board, i - 1, j, trie, m, res, s);
if (i + 1 < board.size() && board[i + 1][j]) Search(board, i + 1, j, trie, m, res, s);
if (j > 0 && board[i][j - 1]) Search(board, i, j - 1, trie, m, res, s);
if (j + 1 < board[i].size() && board[i][j + 1]) Search(board, i, j + 1, trie, m, res, s);
board[i][j] = c;
}
};
void print(const vector<string>& a)
{
cout << "[ "; for (auto& e : a) cout << '"' << e << "\" "; cout << "]" << endl;
}
int main()
{
Solution s;
{
vector<vector<char>> board = {
{'o','a','a','n'},
{'e','t','a','e'},
{'i','h','k','r'},
{'i','f','l','v'}
};
vector<string> words = {"oath","pea","eat","rain"};
vector<string> a = s.findWords(board, words);
print(a); // answer: [ "oath" "eat" ]
}
{
vector<vector<char>> board = {
{'b','b','a','a','b','a'},
{'b','b','a','b','a','a'},
{'b','b','b','b','b','b'},
{'a','a','a','b','a','a'},
{'a','b','a','a','b','b'}
};
vector<string> words = {"abbbababaa"};
vector<string> a = s.findWords(board, words);
print(a); // answer: [ "abbbababaa" ]
}
return 0;
}