-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyascab.cpp
137 lines (112 loc) · 3.19 KB
/
easyascab.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
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <algorithm>
std::vector<std::vector<char>> extract_orderings(
const std::vector<std::string>::iterator start,
const std::vector<std::string>::iterator end,
const std::string::size_type depth,
const std::size_t& ALPHABETSIZE)
{
std::vector<char> order;
order.reserve(std::min(static_cast<std::size_t>(std::distance(start, end)), ALPHABETSIZE));
std::vector<std::vector<char>> orderings;
for (auto it = start; it != end; ++it) {
if (it->size() == depth) {
continue;
}
order.push_back((*it)[depth]);
if (order.size() == ALPHABETSIZE + 1) {
std::cout << "IMPOSSIBLE" << std::flush;
exit(0);
}
if (it + 1 != end && (*it)[depth] == (*(it + 1))[depth]) {
auto new_start = it;
auto new_end = it + 1;
while (new_end != end && (*new_start)[depth] == (*new_end)[depth]) {
++new_end;
}
auto new_orderings = extract_orderings(new_start, new_end, depth + 1, ALPHABETSIZE);
for (auto& o : new_orderings) {
if (o.size() > 1) {
orderings.push_back(std::move(o));
}
}
it = new_end - 1;
}
}
if (order.size() > 1) {
orderings.push_back(std::move(order));
}
return orderings;
}
std::unordered_map<char, std::unordered_set<char>>
combine_orderings(std::vector<std::vector<char>>& orderings)
{
std::unordered_map<char, std::unordered_set<char>> combinations;
for (auto it = orderings.begin(); it != orderings.end(); ++it) {
for (auto itt = it->begin(); itt != it->end(); ++itt) {
combinations[*itt].insert(itt + 1, it->end());
}
}
for (auto& p : combinations) {
std::unordered_set<char> additional;
for (const auto& c : p.second) {
additional.insert(combinations[c].begin(), combinations[c].end());
}
p.second.insert(additional.begin(), additional.end());
}
return combinations;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
char L = 0;
int N = 0;
std::cin >> L >> N;
std::cin.ignore();
const std::size_t ALPHABETSIZE = L - 96;
std::vector<std::string> words(N);
for (auto& w : words) {
std::getline(std::cin, w);
}
auto orderings = extract_orderings(words.begin(), words.end(), 0, ALPHABETSIZE);
for (const auto& order : orderings) {
if (order.size() == ALPHABETSIZE) {
auto sorted_ordering = order;
std::sort(sorted_ordering.begin(), sorted_ordering.end());
if (sorted_ordering.end() != std::unique(sorted_ordering.begin(), sorted_ordering.end())) {
std::cout << "IMPOSSIBLE" << std::flush;
return 0;
}
else {
for (const auto& c : order) {
std::cout << c;
}
std::cout << std::flush;
return 0;
}
}
}
auto combinations = combine_orderings(orderings);
std::vector<char> alphabet(ALPHABETSIZE);
for (const auto& p : combinations) {
if (p.second.end() != std::find(p.second.begin(), p.second.end(), p.first)) {
std::cout << "IMPOSSIBLE" << std::flush;
return 0;
}
alphabet[ALPHABETSIZE - 1 - p.second.size()] = p.first;
}
if (alphabet.end() != std::find(alphabet.begin(), alphabet.end(), 0)) {
std::cout << "AMBIGUOUS" << std::flush;
return 0;
}
for (const auto& c : alphabet) {
std::cout << c;
}
std::cout << std::flush;
return 0;
}