#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
#define INF INT_MAX/2
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
/*
Problem: Type Printer
Topic: Strings, Trie practice
Source: https://oj.uz/problem/view/IOI08_printer
*/
const int N = 500'005; //Number of nodes
const int ALPHA = 26; // Number of characters
int trie[N][ALPHA];
int max_dist[N];
set<int> final_s;
int last_n = 0;
vector<char> ans;
void is_already(string &s){
int curr_node = 0;
int sz = s.size();
for(char it: s){
if(trie[curr_node][it-'a'] == 0){
trie[curr_node][it-'a'] = ++last_n;
}
max_dist[curr_node] = max(max_dist[curr_node], sz--);
curr_node = trie[curr_node][it-'a'];
}
final_s.insert(curr_node);
}
void solve(int node){
set<pii> dist;
for(int i=0; i < 26; i++){
if(trie[node][i] != 0){
dist.insert({max_dist[trie[node][i]], i});
}
}
if(final_s.count(node)) ans.pb('P');
for(auto d: dist){
ans.pb(d.second+'a');
solve(trie[node][d.second]);
}
ans.pb('-');
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
string s;
cin >> n;
while(n--){
cin >> s;
is_already(s);
}
solve(0);
for(int i=ans.size()-1; i >=0; i--){
if(ans[i] == '-') ans.pop_back();
else break;
}
cout << ans.size() << "\n";
for(char c: ans){
cout << c << "\n";
}
return 0;
}
# | 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... |