# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
746976 | khulegub | Type Printer (IOI08_printer) | C++14 | 70 ms | 53060 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;
const int N = 500005;
/** Node count*/
int k = 1;
int to[N][26];
char a[N];
int h[N];
vector<char> ans;
void calc_height(int u) {
int height = 0;
for (int i = 0; i < 26; i++) {
if (to[u][i] != -1) {
int v = to[u][i];
calc_height(v);
height = max(height, 1 + h[v]);
}
}
h[u] = height;
}
void dfs(int u) {
if (u > 0) ans.push_back(a[u]);
vector<pair<int, int> > children;
for (int i = 0; i < 26; i++) {
if (to[u][i] != -1) {
int v = to[u][i];
children.push_back({h[v], v});
}
}
sort(children.begin(), children.end());
for (int i = 0; i < children.size(); i++) {
int v = children[i].second;
dfs(v);
}
if (h[u] == 0) {
ans.push_back('P');
}
if (u > 0) ans.push_back('-');
}
int main() {
int n;
cin >> n;
memset(to, -1, sizeof to);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
int curr = 0;
for (int j = 0; j < s.length(); j++) {
int c = s[j] - 'a';
if (to[curr][c] == -1) {
// Create a new node.
to[curr][c] = k;
k++;
}
a[to[curr][c]] = s[j];
curr = to[curr][c];
}
}
calc_height(0);
dfs(0);
while (ans.size() > 0 && ans.back() == '-') ans.pop_back();
cout << ans.size() << '\n';
for (char c : ans) cout << c << '\n';
}
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... |