# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
716345 | kartapolov | Type Printer (IOI08_printer) | C++14 | 1078 ms | 119988 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
struct vertex {
bool term = 0;
vector<vertex*> children = vector<vertex*>(26, nullptr);
vertex() = default;
void add(const string& s, int i = 0) {
if (this->children[s[i] - 'a'] == nullptr) {
this->children[s[i] - 'a'] = new vertex();
}
if (i + 1 == s.size()) {
(this->children[s[i] - 'a']->term) = true;
} else {
this->children[s[i] - 'a']->add(s, i + 1);
}
}
};
vector<char> ans;
string maxw = "";
int n, k;
void dfs(vertex* v, int h = 0) {
if (v->term) {
ans.push_back('P');
++k;
}
for (int i = 0; i < 26; ++i) {
if (!(h < maxw.size() and i == maxw[h] - 'a') and (v->children[i] != nullptr)) {
ans.push_back(i + 'a');
dfs(v->children[i], h + 1);
if (k != n) ans.push_back('-');
}
}
if (h < maxw.size() and v->children[maxw[h] - 'a'] != nullptr) {
ans.push_back(maxw[h]);
dfs(v->children[maxw[h] - 'a'], h + 1);
if (k != n) ans.push_back('-');
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
vertex root;
for (int i = 0; i < n; ++i) {
string word;
cin >> word;
if (word.size() > maxw.size()) maxw = word;
root.add(word);
}
k = 0;
dfs(&root);
cout << ans.size() << endl;
for (char c : ans) cout << c << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |