-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#115.cc
More file actions
72 lines (61 loc) · 2.48 KB
/
Copy pathLeetCode#115.cc
File metadata and controls
72 lines (61 loc) · 2.48 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
72
class Solution {
private:
void gen(string str, string end,vector<string> tmp,
map<string,vector<string> >& prev, vector<vector<string> >& res){
tmp.push_back(str);
if(str==end){
reverse(tmp.begin(),tmp.end());
res.push_back(tmp);
return;
}
vector<string>& vec = prev[str];
for(int i=0;i<vec.size();i++)
gen(vec[i],end,tmp,prev,res);
}
public:
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<string> > ret;
if(dict.size() <= 1) return ret;
if(dict.find(start)==dict.end() || dict.find(end)==dict.end()) return ret;
vector<pair<string,int> > que;
int head = 0;
que.push_back(make_pair(start,1));
map<string,int> _hash;
map<string,vector<string> > prev;
map<string,vector<string> > li;
for(unordered_set<string>::iterator it = dict.begin();
it!=dict.end();++it){
const string& from = *it;
vector<string>& vec = li[from];
for(int i=0;i<start.length();++i){
string t = from;
for(int j=0;j<26;j++){
t[i] = (char)(j+'a');
if(t!=from && dict.find(t)!=dict.end())
vec.push_back(t);
}
}
}
while(head < que.size()){
if(que[head].first == end){
break;
}
_hash[que[head].first] = que[head].second;
vector<string>& vec = li[que[head].first];
for(int i=0;i<vec.size();i++){
string& t = vec[i];
if(_hash.find(t)==_hash.end() || que[head].second+1 == _hash[t]){
if(_hash.find(t)==_hash.end())
que.push_back(make_pair(t,que[head].second+1));
_hash[t] = que[head].second+1;
prev[t].push_back(que[head].first);
}
}
head++;
}
gen(end,start,vector<string>(),prev,ret);
return ret;
}
};