-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuffixarrayreconstruction.cc
More file actions
99 lines (80 loc) · 2.3 KB
/
suffixarrayreconstruction.cc
File metadata and controls
99 lines (80 loc) · 2.3 KB
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
// William Sjöblom
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
/**
* Suffix.
*/
struct Suffix {
int position;
std::string suffix;
};
const char PLACEHOLDER = '#';
const char WILDCARD = '*';
/**
* Insert 'suffix' into 'target'.
* Returns false if recovery is not possible.
*/
inline bool insert_suffix(std::string& target, Suffix& s) {
bool wildcard = false;
for (int suffix_pos = 0, target_pos = s.position;
target_pos < target.size();
suffix_pos++, target_pos++) {
if (s.suffix[suffix_pos] == WILDCARD) {
wildcard = true;
break;
}
if (target[target_pos] != PLACEHOLDER &&
target[target_pos] != s.suffix[suffix_pos])
return false;
else
target[target_pos] = s.suffix[suffix_pos];
}
if (wildcard) {
for (int suffix_pos = s.suffix.size() - 1, target_pos = target.size() - 1;
s.suffix[suffix_pos] != WILDCARD;
suffix_pos--, target_pos--) {
if (target[target_pos] != PLACEHOLDER &&
target[target_pos] != s.suffix[suffix_pos])
return false;
else
target[target_pos] = s.suffix[suffix_pos];
}
}
return true;
}
/**
* Solve.
* Returns empty string if recovery is not possible.
*/
std::string solve(int length, std::vector<Suffix>& suffixes) {
std::string result;
result.resize(length, PLACEHOLDER);
for (Suffix& s : suffixes) {
if (!insert_suffix(result, s)) return "";
}
for (char c : result) {
if (c == PLACEHOLDER) return "";
}
return result;
}
int main() {
int test_count;
std::cin >> test_count;
while (test_count--) {
int length, suffix_count;
std::cin >> length >> suffix_count;
std::vector<Suffix> suffixes;
suffixes.reserve(suffix_count);
while (suffix_count--) {
Suffix s;
std::cin >> s.position >> s.suffix;
s.position--; // Make position zero indexed.
suffixes.push_back(s);
}
auto result = solve(length, suffixes);
if (result.empty()) printf("IMPOSSIBLE\n");
else printf("%s\n", result.c_str());
}
}