-
Notifications
You must be signed in to change notification settings - Fork 0
/
18_10_1.cpp
92 lines (79 loc) · 2.38 KB
/
18_10_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
//
// 18_10_1.cpp
//
//
// Created by Pengyan Qin on 7/25/15.
//
//
// Given two words(start, end), and a dictionary, find one shortest transformation sequences
// from start to end
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm> // for reverse()
using namespace std;
typedef vector<string> vs;
void BuildPath(map<string, vs> &traces, vs &path, string word, string start){
if(word == start){
path.push_back(word);
reverse(path.begin(), path.end());
return;
}
path.push_back(word);
vs tmp = traces[word];
for(int i = 0; i < tmp.size(); ++i)
BuildPath(traces, path, tmp[i], start);
return;
}
vs word_ladder_onepath(string A[], int n, string start, string end){
map<string, vs> traces; // used to reconstructed the path
vs path; // used to store the reconstructed path
map<string, bool> mymap;
for(int i = 0; i < n; ++i){
mymap[A[i]] = true;
}
vs layer[2];
int cur = 0;
int pre = 1;
layer[cur].push_back(start);
while(!layer[cur].empty()){
for(vs::iterator it = layer[cur].begin(); it != layer[cur].end(); ++it){ // error 1: delete already considered words
mymap[*it] = false;
}
cur = !cur;
pre = !pre;
layer[cur].clear();
for(int i = 0; i < layer[pre].size(); ++i){
string word = layer[pre][i];
for(int j = 0; j < word.size(); ++j){
for(char c = 'a'; c <= 'z'; ++c){
string tmp = word;
if(c != tmp[j]){
tmp[j] = c;
if(mymap[tmp]){
layer[cur].push_back(tmp);
traces[tmp].push_back(word);
if(tmp == end){ // only find one shortest path
BuildPath(traces, path, end, start);
return path;
}
}
}
}
}
}
}
return path;
}
int main(){
string A[] = {"hot", "dot", "dog", "lot", "log", "hit", "cog"};
int n = 7;
string start = "hit";
string end = "cog";
vs path = word_ladder_onepath(A, n, start, end);
for(int i = 0; i < path.size(); ++i){
cout << path[i] << " ";
}
cout << endl;
}