# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
935210 | asdasdqwer | Type Printer (IOI08_printer) | C++14 | 380 ms | 225520 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;
};
vector<Node> trie(500001);
int last = 1;
void insWord(string &s) {
int idx = 0;
for (auto &x:s) {
if (trie[idx].child[x-'a'] == 0) {
trie[idx].child[x-'a'] = last;
trie[last].l = x;
last++;
}
idx = trie[idx].child[x-'a'];
}
}
struct Operation {
char chr;
bool isPrint = false;
bool isBack = false;
};
vector<Operation> out;
void traversal(int index) {
bool foundOne = false;
for (int i=0;i<26;i++) {
if (trie[index].child[i] == 0) continue;
Operation op;op.chr = i + 'a';
out.push_back(op);
traversal(trie[index].child[i]);
op.isBack = true;
out.push_back(op);
foundOne = true;
}
if (!foundOne) {
Operation op;op.isPrint=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);
}
// it's always optimal to stop at the word with the largest length
string s = "";
for (auto &x:v) {
if (x.size() > s.size()) {
s = x;
}
}
int index = 0;
for (auto &x:s) {
int nxt;
for (int i=0;i<26;i++) {
if (trie[index].child[i] != 0 && i != (x-'a')) {
Operation op;op.chr=i+'a';
out.push_back(op);
traversal(trie[index].child[i]);
op.isBack = true;
out.push_back(op);
}
else if (i == (x-'a')) {
nxt = trie[index].child[i];
}
}
Operation op;op.chr=x;
out.push_back(op);
index = nxt;
}
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... |