-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva200.cpp
241 lines (214 loc) · 5.13 KB
/
uva200.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* UVa Online Judge - Problem 200
*/
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
typedef std::set<char> Set;
struct Letter
{
Letter(const char in) {
thisLetter = in;
}
char thisLetter;
Set before;
Set after;
};
typedef std::map<char, Letter*> Map;
typedef std::vector<Letter*> List;
void dump(Letter* l);
void dumpList();
void dump();
Map letterMap;
List letterList;
Letter* getLetter(char letterChar)
{
Map::iterator i = letterMap.find(letterChar);
Letter* ret;
if (i == letterMap.end())
{
ret = new Letter(letterChar);
letterMap.insert(std::pair<char, Letter*>(letterChar, ret));
}
else
{
ret = (*i).second;
}
return ret;
}
void compareLetters(const std::string& prev, const unsigned& prevLength,
const std::string& curr, const unsigned& currLength)
{
unsigned amount = std::min(prevLength, currLength);
for (unsigned comparePlace=0; comparePlace < amount; comparePlace++)
{
if (prev[comparePlace] != curr[comparePlace])
{
Letter* prevLetter = getLetter(prev[comparePlace]);
Letter* currLetter = getLetter(curr[comparePlace]);
prevLetter->after.insert(curr[comparePlace]);
currLetter->before.insert(prev[comparePlace]);
return;
}
}
}
bool compare(Letter* l, Letter* r)
{
// std::cout << "C: " << l->thisLetter << " " << r->thisLetter << std::endl;
if (l->before.size() == 0)
{
return true;
}
if (l->after.size() == 0)
{
return false;
}
if (r->before.size() == 0)
{
return false;
}
if (r->after.size() == 0)
{
return true;
}
if (l->after.find(r->thisLetter) != l->after.end())
{
return true;
}
if (r->before.find(l->thisLetter) != r->before.end())
{
return false;
}
std::cout << "============" << std::endl;
std::cout << "END " << l->thisLetter << " " << r->thisLetter << std::endl;
dump(l);
dump(r);
return true;
}
void sortLetters()
{
//dump();
for (Map::iterator i=letterMap.begin(); i!=letterMap.end(); i++)
{
letterList.push_back((*i).second);
//std::sort(letterList.begin(), letterList.end(), compare);
}
unsigned size = letterList.size();
unsigned loc1 = 0;
unsigned loc2 = 1;
//dumpList();
while (loc2 < size)
{
loc1 = 0;
Letter* right = letterList[loc2];
while (loc1 < loc2)
{
//std::cout << loc1 << " " << loc2 << " ";
//dumpList();
Letter* left = letterList[loc1];
if (left->before.find(right->thisLetter) != left->before.end() ||
right->after.find(left->thisLetter) != right->after.end() ||
right->before.size() == 0)
{
Letter* swap = left;
letterList[loc1] = right;
letterList[loc2] = swap;
//std::cout << "S:" << loc1 << " " << loc2 << " ";
//dumpList();
loc1 = 0;
//loc2 = 1;
right = letterList[loc2];
}
else
{
loc1++;
}
}
loc2++;
}
}
void dump(Letter* l)
{
std::cout << "-------" << std::endl;
std::cout << "Letter: " << l->thisLetter << std::endl;
std::cout << "Before: ";
for (Set::iterator i2=l->before.begin();
i2!=l->before.end(); i2++)
{
std::cout << *i2 << " ";
}
std::cout << std::endl;
std::cout << "After: ";
for (Set::iterator i2=l->after.begin();
i2!=l->after.end(); i2++)
{
std::cout << *i2 << " ";
}
std::cout << std::endl;
}
void dumpList()
{
std::cout << "-------" << std::endl;
std::cout << "List: ";
for (List::iterator i=letterList.begin(); i!=letterList.end(); i++)
{
std::cout << (*i)->thisLetter << " ";
}
std::cout << std::endl;
}
void dump()
{
std::cout << "========" << std::endl;
std::cout << "Size: " << letterMap.size() << std::endl;
for (Map::iterator i=letterMap.begin(); i!=letterMap.end(); i++)
{
Letter* l = (*i).second;
dump(l);
}
dumpList();
}
void output()
{
for (List::iterator i=letterList.begin(); i!=letterList.end(); i++)
{
std::cout << (*i)->thisLetter;
}
std::cout << std::endl;
}
int main()
{
std::string prev;
unsigned prevLength = 0;
std::string input = "";
while (std::cin.good())
{
//dump();
prev = input;
std::cin >> input;
if (std::cin.fail())
{
return 0;
}
if (input[0] == '#')
{
//dump();
sortLetters();
//dump();
output();
return 0;
}
if (prev == "")
{
prevLength = input.length();
continue;
}
unsigned length = input.length();
if (length > 0)
{
compareLetters(prev, prevLength, input, length);
}
prevLength = length;
}
}