-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP200.cpp
61 lines (59 loc) · 1.19 KB
/
P200.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
#include <iostream>
#include <stdio.h>
int main() {
bool inUse[26];
int in[26];
bool before[26*26];
for(int i = 0; i < 26; ++i) {
inUse[i] = false;
in[i] = 0;
}
for(int i = 0; i < 26*26; ++i) {
before[i] = false;
}
std::string word, prev;
while(true) {
std::cin >> word;
if(word[0] == '#')
break;
for(unsigned int i = 0; i < word.size(); ++i) {
inUse[word[i]-'A'] = true;
}
for(unsigned int i = 0; i < word.size() && i < prev.size(); ++i) {
if(prev[i] != word[i]) {
int fromI = prev[i]-'A';
int toI = word[i]-'A';
if(!before[26*fromI+toI]) {
before[26*fromI+toI] = true;
++in[toI];
//std::cerr << "Setting relation: " << prev[i] << " -> " << word[i] << std::endl;
}
break;
}
}
prev = word;
}
int noIn = -1;
for(int i = 0; i < 26; ++i) {
if(inUse[i] && in[i] == 0) {
noIn = i;
break;
}
}
while(noIn != -1) {
std::cout << (char)(noIn+'A');
int newNoIn = -1;
for(int i = 0; i < 26; ++i) {
if(i == noIn)
continue;
if(before[noIn*26+i]) {
--in[i];
if(in[i] == 0)
newNoIn = i;
}
}
noIn = newNoIn;
}
std::cout << std::endl;
return 0;
}