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;
queue <char> q;
int cnt=0;
char letter[500005];
int nxt[500005][26];
bool mark[500005];
int kraj[500005];
void addtrie(string s, int k){
int v = 0;
int x = s.size();
for(int i=0; i<x; i++){
int ch = s[i]-'a';
if(!nxt[v][ch]) nxt[v][ch] = ++cnt;
v = nxt[v][ch];
letter[v] = ch+'a';
if(k == 1) mark[v] = 1;
}
kraj[v] = 1;
}
void dfs(int v){
if(v) q.push(letter[v]);
if(kraj[v]){q.push('P'); kraj[v] = 0;}
for(int i=0; i<26; i++){
if(nxt[v][i] && !mark[nxt[v][i]]) dfs(nxt[v][i]);
}
for(int i=0; i<26; i++){
if(nxt[v][i] && mark[nxt[v][i]]) dfs(nxt[v][i]);
}
if(!mark[v] && v) q.push('-');
}
bool cmp(string a, string b){
return a.size() < b.size();
}
string str[25005];
int main(){
int n;
cin >> n;
for(int i=1; i<=n; i++) cin >> str[i];
sort(str+1, str+1+n, cmp);
for(int i=1; i<n; i++){
addtrie(str[i], 0);
}
addtrie(str[n], 1);
dfs(0);
cout << q.size() << "\n";
while(!q.empty()){
cout << q.front() << "\n";
q.pop();
}
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... |