# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
550895 | Jomnoi | Type Printer (IOI08_printer) | C++17 | 44 ms | 35532 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>
#define DEBUG false
using namespace std;
vector <char> ans;
class Node {
public :
Node *c[26];
int sz;
bool finish;
Node() : finish(false), sz(1) {
for(int i = 0; i < 26; i++) {
c[i] = NULL;
}
}
};
void add(Node *node, string s, int idx = 0) {
if(idx == s.length()) {
node->finish = true;
return;
}
int v = s[idx] - 'a';
if(node->c[v] == NULL) {
node->c[v] = new Node();
}
add(node->c[v], s, idx + 1);
node->sz = 1;
for(int i = 0; i < 26; i++) {
if(node->c[i] != NULL) {
node->sz += node->c[i]->sz;
}
}
}
void solve(Node *node) {
if(node->finish == true) {
ans.push_back('P');
return;
}
vector <pair <int, int>> vec;
for(int v = 0; v < 26; v++) {
if(node->c[v] != NULL) {
vec.emplace_back(node->c[v]->sz, v);
}
}
sort(vec.begin(), vec.end());
for(auto [l, v] : vec) {
ans.push_back(v + 'a');
solve(node->c[v]);
ans.push_back('-');
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
Node *root = new Node();
for(int i = 1; i <= N; i++) {
string s;
cin >> s;
add(root, s);
}
solve(root);
while(ans.back() == '-') {
ans.pop_back();
}
cout << ans.size() << '\n';
for(auto c : ans) {
cout << c << '\n';
}
return 0;
}
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... |