# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1201295 | ziad3ssam10 | Type Printer (IOI08_printer) | C++20 | 1 ms | 328 KiB |
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define endl '\n'
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define wady ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void files() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
struct Trie {
struct Node {
int child[26] = {}, cnt = 0;
bool done = 0, longest = 0;
};
vector<Node> trie;
Trie() { trie.emplace_back(); }
void add(string &s, int l) {
int cur = 0;
for (auto &c: s) {
if (!trie[cur].child[c - 'a']) {
trie[cur].child[c - 'a'] = trie.size();
trie.emplace_back();
}
cur = trie[cur].child[c - 'a'];
if (!l)trie[cur].cnt++;
trie[cur].longest = l;
}
trie[cur].done = 1;
}
string ans = "";
void dfs(int node) {
if (trie[node].done)
ans.push_back('P');
int temp = -1;
for (int i = 0; i < 26; i++) {
int nxt = trie[node].child[i];
if (nxt) {
if (trie[nxt].longest) temp = i;
else {
ans.push_back('a' + i);
dfs(nxt);
ans.push_back('-');
}
}
}
if (~temp) {
ans += temp + 'a';
dfs(trie[node].child[temp]);
ans += '-';
}
}
void output() {
while (ans[ans.size() - 1] == '-')ans.pop_back();
cout << ans.size() << endl;
for (auto ch: ans)cout << ch << endl;
}
};
void solve(int tc) {
int n;
cin >> n;
vector<string> a(n);
string lon = "";
Trie trie;
for (auto &s: a)cin >> s;
sort(a.begin(), a.end());
for (auto &i: a) {
trie.add(i, 0);
if (i.size() > lon.size())lon = i;
}
trie.add(lon, 1);
trie.dfs(0);
trie.output();
}
signed main() {
wady
files();
int tt = 1;
//cin >> tt;
while (tt--) solve(tt);
}
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... |