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 N = 5e5 + 5;
const int D = 26;
int trie[N][D];
int node = 1;
bool fin[N];
bool vis[N];
vector<char> ans;
int great;
int low[N];
priority_queue<tuple<int, int, char>> G[N];
void add(string s){
int cur = 0;
for(char ch : s){
int c = ch - 'a';
if(!trie[cur][c]){
trie[cur][c] = node;
node++;
}
cur = trie[cur][c];
}
fin[cur] = 1;
}
void produndidad(int cur){
if(fin[cur]) ans.push_back('P');
while(!G[cur].empty()){
int hijo;
char c;
tie(great, hijo, c) = G[cur].top();
G[cur].pop();
ans.push_back(c);
produndidad(hijo);
ans.push_back('-');
}
}
void dfs(int cur, int he){
vis[cur] = 1;
low[cur] = he;
for(int c = 0; c < 26; c++){
if(trie[cur][c] and !vis[trie[cur][c]]){
dfs(trie[cur][c], he+1);
int child = trie[cur][c];
G[cur].push({-low[child], child, c+'a'});
low[cur] = max(low[cur], low[child]);
}
}
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
string s;
cin >> s;
add(s);
}
dfs(0, 0);
produndidad(0);
cout << ans.size() + great << "\n";
for(int i = 0; i < (int)ans.size() + great; i++){
cout << ans[i] << "\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... |