이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |