| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1311500 | Lakshya108 | Type Printer (IOI08_printer) | C11 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
static const int A = 26;
static const int MAXN = 500000;
struct Node {
int nxt[A];
int end;
Node() {
memset(nxt, -1, sizeof nxt);
end = 0;
}
};
Node tr[MAXN];
int ptr = 0;
int new_node() {
++ptr;
tr[ptr] = Node();
return ptr;
}
void add_word(const string &s) {
int u = 0;
for (char c : s) {
int x = c - 'a';
if (tr[u].nxt[x] == -1)
tr[u].nxt[x] = new_node();
u = tr[u].nxt[x];
}
tr[u].end++;
}
void dfs(int u, const string &mx, int d, vector<char> &out) {
if (tr[u].end) out.push_back('P');
int keep = -1;
if (d < (int)mx.size())
keep = mx[d] - 'a';
for (int i = 0; i < A; i++) {
if (i == keep) continue;
int v = tr[u].nxt[i];
if (v != -1) {
out.push_back(char('a' + i));
dfs(v, mx, d + 1, out);
}
}
if (keep != -1 && tr[u].nxt[keep] != -1) {
out.push_back(char('a' + keep));
dfs(tr[u].nxt[keep], mx, d + 1, out);
}
out.push_back('-');
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<string> s(n);
for (auto &x : s) {
cin >> x;
add_word(x);
}
sort(s.begin(), s.end(),
[](const string &a, const string &b) {
return a.size() > b.size();
});
vector<char> ans;
dfs(0, s[0], 0, ans);
while (!ans.empty() && ans.back() == '-') ans.pop_back();
cout << ans.size() << '\n';
for (char c : ans) cout << c << '\n';
return 0;
}
