-
Notifications
You must be signed in to change notification settings - Fork 0
/
L126_WordLadderII.cpp
88 lines (83 loc) · 2.64 KB
/
L126_WordLadderII.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
#pragma warning(disable:4786)
#pragma warning(disable:4503)
/*
url: leetcode.com/problems/word-ladder-ii
too many advanced data structure
use cpp
Solution: AC 259ms 51.41%
*/
#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
void find(string t, string s, vector<string >& v, map<string, vector<string > >& m, vector<vector<string > >& ans) {
v.insert(v.begin(), t);
if (! t.compare(s)) {
ans.push_back(v);
} else if (m.count(t)) {
for (vector<string >::iterator iter = m[t].begin(); iter != m[t].end(); iter ++)
find(*iter, s, v, m, ans);
}
v.erase(v.begin());
}
vector<vector<string > > findLadders(string s, string t, vector<string>& w) {
vector<vector<string > > ans;
int sn = s.size() , i;
set<string > nv , hv;
for (i = 0; i < w.size(); i ++) nv.insert(w[i]);
map<string, vector<string > > m;
queue<string > q;
nv.insert(s);
q.push(s);
bool isFind = false;
while (! q.empty()) {
int size = q.size();
while (size -- > 0) {
string n = q.front();
string v = n;
q.pop();
for (i = 0; i < sn; i ++) {
for (char c = 'a'; c <= 'z'; c ++) {
v[i] = c;
if (! nv.count(v)) continue;
if (! hv.count(v)) {
hv.insert(v);
q.push(v);
}
m[v].push_back(n);
if (! v.compare(t)) isFind = true;
}
v[i] = n[i];
}
}
if (isFind) break;
for (set<string >::iterator iter = hv.begin(); iter != hv.end(); iter ++)
nv.erase(*iter);
hv.clear();
}
vector<string > v;
find(t, s, v, m, ans);
return ans;
}
};
int main() {
string g[] = {"hot","dot","dog","lot","log","cog"};
string s = "hit", t = "cog";
vector<string > w;
int i;
for (i = 0; i < (sizeof(g)/sizeof(g[0])); i ++) w.push_back(g[i]);
vector<vector<string > > ans = Solution().findLadders(s, t, w);
for (i = 0; i < ans.size(); i ++) {
cout<<"+++++++++"<<endl;
for (int j = 0; j < ans[i].size(); j ++) {
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
return 0;
}