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;
const int SIGMA = 26;
int ncnt = 0;
struct node{
int nxt[SIGMA] = {};
bool word = false;
int heavy = -1;
node() {}
int extend(int c){
if(!nxt[c]) nxt[c] = ++ncnt;
return nxt[c];
}
int extend_heavy(int c){
heavy = c;
return nxt[c];
}
};
const int MAXN = 25011;
node trie[MAXN * 21];
string ans;
void dfs(int root){
if(trie[root].word) ans.push_back('P');
for(int c = 0; c < SIGMA; c++){
if(trie[root].nxt[c] && trie[root].heavy != c){
ans.push_back('a' + c);
dfs(trie[root].nxt[c]);
ans.push_back('-');
}
}
if(trie[root].heavy != -1){
ans.push_back('a' + trie[root].heavy);
dfs(trie[root].nxt[trie[root].heavy]);
ans.push_back('-');
}
}
int root = 0;
int main(){
cin.tie(0); cout.tie(0)->sync_with_stdio(false);
int n; cin >> n;
vector<string> vs;
for(int i = 0; i < n; i++){
string s; cin >> s;
vs.push_back(s);
int x = root;
for(auto c : s){
x = trie[x].extend(c - 'a');
}
trie[x].word = true;
}
int idx = 0;
for(int i = 1; i < n; i++){
if(vs[i].size() > vs[idx].size()){
idx = i;
}
}
{
int x = root;
for(auto c : vs[idx]){
x = trie[x].extend_heavy(c - 'a');
}
}
dfs(root);
while(ans.back() == '-') ans.pop_back();
cout << ans.size() << endl;
for(auto c : ans) cout << c << endl;
}
# | 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... |