-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path281_1_19_2.cpp
145 lines (127 loc) · 3.48 KB
/
281_1_19_2.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<utility>
#include<stack>
using namespace std;
vector<pair<string,pair<int,int>>> Record;
int position = 0;
pair<string,pair<int,int>> MakePair(string S, int Prev, int Post)
{
pair<int,int> int_pair(Prev,Post);
pair<string,pair<int,int>> result(S,int_pair);
return result;
}
void OneLetterLength_M(string StartWord, string EndWord, vector<string> &Dict, queue<pair<string,pair<int,int>>> &Result, bool &FirstTime, int &Index, bool &Found)
{
cout << "This is a new recursion" <<endl;
if (FirstTime == true)
{
FirstTime = false;
Dict.push_back("#");
Result.push(MakePair(StartWord,0,0));
Record.push_back(MakePair(StartWord,0,0));
}
if (Result.empty())
{
return;
}
//Record.push_back(Result.front());
Result.pop();
int index = 0;
while (Dict[index] != "#")
{
if (Dict[index].size() == StartWord.size() - 1)
{
// to do: this below can be updated into a sub function to call
for (int i = 0; i < StartWord.size(); i++)
{
string temp = StartWord;
if (temp.erase(i,1) == Dict[index]) // Found the target "delete mode"
{
position++;
cout << "the matched word is: " << Dict[index] << ", at position: " << position << endl;
Result.push(MakePair(Dict[index], Index, position));
string tmp = "d," + to_string(i) + "," + StartWord[i];
Record.push_back(MakePair(tmp,Index,position));
if (Dict[index] == EndWord)
{
cout << "1found: " << Dict[index] << ", position:" << position << endl;
Found = true;
//Record.push_back(MakePair(Dict[index],Index,position));
return;
}
Dict.erase(Dict.begin()+index);
break;
}
}
}
else if (Dict[index].size() == StartWord.size() + 1)
{
for (int i = 0; i < Dict[index].size(); i++)
{
string temp = Dict[index];
if (temp.erase(i,1) == StartWord) // Found the target "insert mode"
{
position++;
cout << "the matched word is: " << Dict[index] << ", at position: " << position << endl;
Result.push(MakePair(Dict[index], Index, position));
string tmp = "i," + to_string(i) + "," + Dict[index][i];
Record.push_back(MakePair(tmp,Index,position));
if (Dict[index] == EndWord)
{
cout << "2found: " << Dict[index] << ", position:" << position << endl;
Found = true;
//Record.push_back(MakePair(Dict[index],Index,position));
return;
}
Dict.erase(Dict.begin()+index);
break;
}
}
}
index++;
}
pair<string,pair<int,int>> next = Result.front();
OneLetterLength_M(next.first, EndWord, Dict, Result, FirstTime, next.second.second, Found);
return;
}
int main()
{
vector<string> Dict;
Dict.push_back("ca");
Dict.push_back("kate");
Dict.push_back("cate");
Dict.push_back("cae");
Dict.push_back("cte");
Dict.push_back("kcte");
string start = "cat";
string end = "kcte";
queue<pair<string,pair<int,int>>> test;
bool first = true;
int first_index = 0;
bool found = false;
OneLetterLength_M(start, end, Dict, test, first, first_index,found);
if (found == true)
{
stack<string> Final;
//cout << Record.size() << endl;
//cout << test.back().first << endl; //bugs
int post = test.back().second.second;
do
{
//cout << "post:" << post << endl;
Final.push(Record[post].first);
post = Record[post].second.first;
} while (post != 0);
Final.push(Record[0].first);
cout << Final.size() << endl;
while (!Final.empty())
{
cout << Final.top() << endl;
Final.pop();
}
}
return 0;
}