-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathladder program.cpp
120 lines (95 loc) · 3.35 KB
/
ladder program.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
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <vector>
#include <queue>
#include <set>
using namespace std;
const int first_length = 26;
void getWords(string &Firstword, string &Secondword);
void printWordLadder(string Firstword, string Secondword);
int main() {
string Firstword, Secondword;
getWords(Firstword, Secondword);
printWordLadder(Firstword, Secondword);
return 0;
}
void getWords(string &Firstword, string &Secondword) {
while (true) {
cout << "Enter any word: ";
cin>>Firstword;
cout << "Enter any another word of the same length: ";
cin>>Secondword;
if (Firstword.length() == Secondword.length()) {
break;
}
cout << "Please enter two words with the same length." << endl;
}
}
void printWordLadder(string Firstword, string Secondword){
//Empty queue of stacks
queue<stack<string> > myQueue;
//Stack which will contain a final word ladder
stack<string> wordladder;
// creates and adds a stack containing Firstword to the queue
stack<string> myStack;
myStack.push(Firstword);
myQueue.push(myStack);
// creates two sets: one for the dictionary and one for the tested words
string token;
ifstream dictionary("EnglishWords.dat");
set<string> myDictionary;
set<string> testedWords;
if(dictionary.is_open()) {
while (dictionary >> token) {
myDictionary.insert(token);
}
// while the queue is not empty:
while (!(myQueue.empty())) {
// dequeue the partial-ladder stack from the front of the queue.
stack<string> ladder = myQueue.front();
myQueue.pop();
string word = ladder.top();
// if the word on top of the stack is the destination word:
if (word == Secondword) {
// output the elements of the stack as the solution.
cout << "The ladder from " << Firstword << " to " << Secondword << " is \n";
//Copy the ladder stack to worldladder to take it in the order.
while(!ladder.empty()){
wordladder.push(ladder.top());
ladder.pop();
}
while(!wordladder.empty()){
cout<<wordladder.top()<<" > ";
wordladder.pop();
}
}
else {
// for each valid English word that is a "neighbor" (differs by 1 letter) of the word on top of the stack:
string test;
for (int i = 0; i < word.size(); i++) {
for (char j = 'a'; j <= 'z'; j++) {
test = word.substr(0, i) + j + word.substr(i + 1);
// if that word is an english word
if (myDictionary.count(test)) {
// if that neighbor word has not already been used in a ladder before:
if (!testedWords.count(test)) {
// create a copy of the current ladder stack.
stack<string> copy = ladder;
// put the neighbor word on top of the copy stack.
copy.push(test);
// add the copy stack to the end of the queue.
myQueue.push(copy);
}
}
// Add test to tested words because it is already used.
testedWords.insert(test);
}
}
}
}
} else {
cerr << "Can't access the dictionary'" << endl;
}
}