# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
935220 | asdasdqwer | Type Printer (IOI08_printer) | C++14 | 92 ms | 59328 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 {
char l;
map<char,int> child;
bool isEnd = false;
};
vector<Node> trie(500001);
int last = 1;
void insWord(string &s) {
int idx = 0;
for (auto &x:s) {
if (trie[idx].child.find(x) == trie[idx].child.end()) {
trie[idx].child[x] = last;
trie[last].l = x;
last++;
}
idx = trie[idx].child[x];
}
trie[idx].isEnd = true;
}
struct Operation {
char chr;
bool isPrint = false;
bool isBack = false;
};
vector<Operation> out;
void traversal(int index) {
if (trie[index].isEnd) {
Operation op;op.isPrint=true;
out.push_back(op);
}
for (auto [chr, idx]:trie[index].child) {
Operation op;op.chr = chr;
out.push_back(op);
traversal(idx);
op.isBack = true;
out.push_back(op);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<string> v(n);
for (auto &x:v)cin>>x;
trie[0].l = ' ';
for (auto x:v) {
insWord(x);
}
string s = "";
for (auto &x:v) {
if (x.size() > s.size()) {
s = x;
}
}
int index = 0;
for (auto &x:s) {
int nxt;
for (auto [chr, idx] : trie[index].child) {
if (chr != x) {
Operation op;op.chr=chr;
out.push_back(op);
traversal(idx);
op.isBack = true;
out.push_back(op);
}
else {
nxt = idx;
}
}
Operation op;op.chr=x;
out.push_back(op);
index = nxt;
if (trie[index].isEnd) {
Operation op;op.isPrint=true;
out.push_back(op);
}
}
cout<<out.size()<<"\n";
for (auto &x:out) {
if (x.isBack) {
cout<<"-\n";
}
else if (x.isPrint) {
cout<<"P\n";
}
else {
cout<<x.chr<<"\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... |