# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
802987 | huantran | Type Printer (IOI08_printer) | C++17 | 170 ms | 230596 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 <algorithm>
#include <cstring>
#include <string.h>
#include <vector>
#include <map>
#define bit(x, i) ((x >> i) & 1LL)
using namespace std;
typedef long long int ll;
const int maxn = 1e6 + 5;
int n, num = 0;
vector<char> ans;
struct Vertex{
ll next[27];
ll output = 0;
ll sz;
};
Vertex trie[maxn];
ll cnt = 1;
void add_edge(string s)
{
ll u = 0;
for(auto c : s)
{
if(!trie[u].next[c - 'a'])
{
trie[u].next[c - 'a'] = cnt++;
}
u = trie[u].next[c - 'a'];
}
trie[u].output++;
}
void trie_size(int u)
{
trie[u].sz = 1;
for(int i = 0; i <= 25; i++)
{
if(trie[u].next[i] != 0)
{
trie_size(trie[u].next[i]);
trie[u].sz = max(trie[u].sz, trie[trie[u].next[i]].sz + 1);
}
}
}
void get_ans(int u)
{
if(trie[u].output)
{
for(int i = 1; i <= trie[u].output && num < n; i++)
{
ans.push_back('P');
num++;
}
}
vector<pair<ll, pair<ll, ll>>> e;
for(int i = 0; i <= 25; i++)
{
if(trie[u].next[i] != 0)
e.push_back({trie[trie[u].next[i]].sz, {trie[u].next[i], i}});
}
sort(e.begin(), e.end());
for(int i = 0; i < e.size(); i++)
{
ans.push_back((char)('a' + e[i].second.second));
get_ans(e[i].second.first);
if(num < n)
ans.push_back('-');
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
ll leng = 0;
string t;
for(int i = 1; i <= n; i++)
{
string s;
cin >> s;
add_edge(s);
}
trie_size(0);
get_ans(0);
cout << ans.size() << '\n';
for(auto j : ans)
cout << j << '\n';
}
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... |