# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1084321 | HaciyevAlik | Type Printer (IOI08_printer) | C++14 | 165 ms | 99608 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 <bits/stdc++.h>
using namespace std;
#define int long long
#define oo 1000000000000000
const int mx = 1e6 + 5;
int trie[mx][26], d[mx], cnt[mx];
int last, node;
vector<char> v;
bool f;
pair<int,int> null = {-1,-1};
void insert(string s) {
int cur = 0;
for(int i = 0; i < s.size(); ++i) {
if(!trie[cur][s[i]-'a']) {
trie[cur][s[i]-'a'] = ++last;
}
cur = trie[cur][s[i]-'a'];
}
cnt[cur]++;
}
void dfsd(int u) {
for(int i = 0; i < 26; ++i) {
if(trie[u][i]) {
dfsd(trie[u][i]);
d[u] = max(d[u],d[trie[u][i]] + 1);
}
}
}
void dfsn(int u) {
int cur = -1;
for(int i = 0; i < 26; ++i) {
if(trie[u][i]) {
cur = max(cur,d[trie[u][i]]);
}
}
for(int i = 0; i < 26; ++i) {
if(trie[u][i] > 0 && d[trie[u][i]] == cur) {
dfsn(trie[u][i]);
}
}
if(cur == -1) {
node = u;
}
}
void dfs(int u) {
if(f) return;
while(cnt[u] > 0) {
v.push_back('P');
cnt[u]--;
}
int cur = -1;
for(int i = 0; i < 26; ++i) {
if(!trie[u][i]) continue;
cur = max(cur,d[trie[u][i]]);
}
pair<int,int> lastgo = null;
for(int i = 0; i < 26; ++i) {
if(!trie[u][i]) continue;
if(d[trie[u][i]] == cur) lastgo = {trie[u][i],i};
}
for(int i = 0; i < 26; ++i) {
if(trie[u][i] && trie[u][i] != lastgo.first) {
v.push_back(char('a' + i));
dfs(trie[u][i]);
}
}
if(lastgo != null) {
v.push_back(char('a' + lastgo.second));
dfs(lastgo.first);
}
if(u == node) {
f = 1;
}
if(!f) v.push_back('-');
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n;
for(int i = 1; i <= n; ++i) {
string s; cin >> s;
insert(s);
}
dfsd(0);
dfsn(0);
dfs(0);
cout << v.size() << '\n';
for(char ch : v) {
cout << ch << '\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... |