# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1262771 | tminh | Type Printer (IOI08_printer) | C++20 | 107 ms | 99220 KiB |
#include "bits/stdc++.h"
using namespace std;
#define task ""
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ep emplace_back
#define pb push_back
#define pf push_front
const ll mod = 1e9 + 7;
const int N = 1e5 + 5;
const ll oo = 1e18;
bool START;
struct node {
node *child[26];
int need, depth;
node() {
for (int i = 0; i < 26; ++i) child[i] = nullptr;
need = depth = 0;
}
};
node* root = new node();
int n;
void add(const string s) {
node* p = root;
for (char ch : s) {
if (p->child[ch - 'a'] == nullptr) p->child[ch - 'a'] = new node();
p = p->child[ch - 'a'];
p->depth = max(p->depth, sze(s));
}
++p->need;
}
string ans = "";
void dfs(node* u) {
for (int i = 1; i <= u->need; ++i) ans += 'P';
vector<pii> adj;
for (int i = 0; i < 26; ++i) {
if (u->child[i] == nullptr) continue;
adj.pb(make_pair(u->child[i]->depth, i));
}
sort(vall(adj));
for (auto [d, i] : adj) {
ans += char(i + 'a');
dfs(u->child[i]);
ans += '-';
}
}
inline void solve() {
dfs(root);
while(!ans.empty() && ans.back() == '-') ans.pop_back();
cout << sze(ans) << endl;
for (char ch : ans) cout << ch << endl;
}
string s;
inline void input() {
cin >> n;
while(n--) {
cin >> s;
add(s);
}
return solve();
}
bool END;
int main() {
if(fopen(task ".inp", "r")) {
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
input();
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
cerr << "Memory: " << fabs ((&END - &START)) / 1048576.0 << "MB\n";
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... |