# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
716348 | kartapolov | Type Printer (IOI08_printer) | C++14 | 374 ms | 119672 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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define print(x) cout << #x << " = " << x << endl;
#define input(vec) for (size_t i = 0; i < vec.size(); i++) cin >> vec[i];
const long long MOD = 1000000000 + 7;
using namespace std;
using ll = long long;
vector<char> ans;
class BOR {
struct Node {
vector<Node*> kids = vector<Node*>(26, nullptr);
bool isTerminal = false;
bool isLeave = true;
int length = 1000;
};
public:
Node *head = new Node();
void add(Node *cur, int i, string& s) {
if (i == s.size()) {
cur->isTerminal = true;
return;
}
if (cur->kids[s[i] - 'a'] == nullptr) {
cur->kids[s[i] - 'a'] = new Node();
cur->isLeave = false;
}
add(cur->kids[s[i] - 'a'], i + 1, s);
}
void output(Node *cur, char data) {
if (cur == nullptr) return;
for (int i = 0 ; i < 26; i++) output(cur->kids[i], 'a' + i);
cout << data << "->"<<cur->isTerminal<<"->" << cur->length << ' ';
}
int whatLength(Node *cur) {
return (cur == nullptr ? 0 : cur->length);
}
void count(Node *cur) {
if (cur == nullptr) return;
if (cur->isLeave) {
cur->length = 1;
return;
}
for (auto kid : cur->kids) count(kid);
for (auto kid : cur->kids) cur->length = max(cur->length, whatLength(kid) + 1);
return;
}
void count() {
count(this->head);
return;
}
void dfs(Node *cur, char data) {
if (cur == nullptr) return;
ans.push_back(data);
if (cur->isTerminal) ans.push_back('P');
vector<pair<int, int>> fake(cur->kids.size());
for (int i = 0; i < cur->kids.size(); i++)
fake[i] = {whatLength(cur->kids[i]), i};
sort(fake.begin(), fake.end());
for (int i = 0; i < fake.size(); i++) dfs(cur->kids[fake[i].second], 'a' + fake[i].second);
ans.push_back('-');
return;
}
};
void solve(BOR& S) {
int n, t; cin >> n; t = n;
while (t--) {
string s; cin >> s;
S.add(S.head, 0, s);
}
S.count();
//S.output(S.head, '*'); cout << endl;
S.dfs(S.head, '*');
int q = 0;
for (int i = ans.size(); i >= 0 && ans[i] != 'P'; i--) q++;
cout << ans.size() - q << '\n';
for (int i = 1; i <= ans.size() - q; i++)
cout << ans[i] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
BOR S;
int t=1; //cin >> t;
while (t--) solve(S);
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... |