# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
659636 | pakapu | Type Printer (IOI08_printer) | C++17 | 120 ms | 101568 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 node {
node* c[26];
bool end = false;
bool marked = false;
node() {
for(int i = 0; i < 26; i++) {
c[i] = nullptr;
}
}
};
vector<char> result;
struct trie {
node* head;
trie() {
head = new node;
head -> marked = true;
}
void add(string s, bool m) {
node* curr = head;
for(int i = 0; i < s.size(); i++) {
if(curr -> c[s[i] - 'a'] == nullptr) {
curr -> c[s[i] - 'a'] = new node;
}
curr = curr -> c[s[i] - 'a'];
curr -> marked = m;
}
curr -> end = true;
}
void out() {
out(head, nullptr);
}
void out(node* v, node* prev) {
if(v -> end) {
result.push_back('P');
}
int mi = -1;
for(int i = 0; i < 26; i++) {
if(v -> c[i] != nullptr) {
if(v -> c[i] -> marked) {
mi = i;
continue;
}
result.push_back((char)(i + 'a'));
out(v -> c[i], v);
}
}
if(mi != -1) {
result.push_back((char)(mi + 'a'));
out(v -> c[mi], v);
}
if(!v -> marked) {
result.push_back('-');
}
}
};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<string> a(n);
string mx = "";
for(int i = 0; i < n; i++) {
cin >> a[i];
if(a[i].size() > mx.size()) {
mx = a[i];
}
}
trie t;
for(auto c : a) {
t.add(c, false);
}
t.add(mx, true);
t.out();
assert(result.back() == 'P');
cout << result.size() << '\n';
for(auto c : result) {
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... |