# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1104223 | dzhoz0 | Type Printer (IOI08_printer) | C++17 | 142 ms | 232568 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.
/*
ghmt the cutie :3
UwU
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1e18
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>
const int MOD = 1'000'000'000 + 7;
void setIO(string name = "")
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#ifdef LOCAL
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
if (!name.empty())
{
freopen((name + ".INP").c_str(), "r", stdin);
freopen((name + ".OUT").c_str(), "w", stdout);
}
#endif
}
const int MAXNODES = 1e6;
struct Node {
int child[26];
int cnt;
bool stop;
int mx;
Node() {
memset(child, -1, sizeof(child));
stop = 0;
cnt = 0;
mx = 0;
}
};
Node trie[MAXNODES + 5];
int sz = 0;
void insert(string &s) {
int pos = 0;
for(char c : s) {
if(trie[pos].child[c - 'a'] == -1) {
trie[pos].child[c - 'a'] = ++sz;
}
pos = trie[pos].child[c - 'a'];
trie[pos].cnt++;
trie[pos].mx = max(trie[pos].mx, (int)s.size());
}
trie[pos].stop = 1;
}
vector<char> ops;
void traverse(int id) {
if(trie[id].stop == 1) ops.push_back('P');
vector<pair<int, char>> v;
for(char c = 'a'; c <= 'z'; c++) {
if(trie[id].child[c - 'a'] != -1) {
// ops.push_back(c);
// traverse(trie[id].child[c - 'a']);
v.push_back({trie[trie[id].child[c - 'a']].mx, c});
}
}
sort(v.begin(), v.end());
for(auto it : v) {
ops.push_back(it.s);
traverse(trie[id].child[it.s - 'a']);
}
ops.push_back('-');
}
void solve()
{
int n; cin >> n;
vector<string> a(n);
for(string &s : a) {
cin >> s;
insert(s);
}
traverse(0);
while(ops.back() != 'P') ops.pop_back();
cout << ops.size() << '\n';
for(char c : ops) cout << c << '\n';
}
signed main()
{
setIO();
int t = 1;
// cin >> t;
while (t--)
solve();
}
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... |