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 K = 26;
struct Node {
int next[K];
int mx_depth = 0;
bool output = false;
Node() {
fill(next, next+K, -1);
}
};
vector<Node> trie(1);
void add_string(string const& s) {
int v = 0;
stack<int> st;
for (char ch : s) {
st.push(v);
int c = ch - 'a';
if (trie[v].next[c] == -1) {
trie[v].next[c] = trie.size();
trie.emplace_back();
}
v = trie[v].next[c];
}
trie[v].output = true;
while (!st.empty()) {
v = st.top();
st.pop();
for (int i = 0; i < K; ++i) {
if (~trie[v].next[i])
trie[v].mx_depth = max(trie[v].mx_depth, trie[trie[v].next[i]].mx_depth+1);
}
}
}
vector<char> ans;
void dfs(int v = 0) {
if (trie[v].output) ans.push_back('P');
set<array<int, 3>> st;
for (int i = 0; i < K; ++i) {
if (~trie[v].next[i]) st.insert({trie[trie[v].next[i]].mx_depth, trie[v].next[i], i});
}
while (!st.empty()) {
auto it = *st.begin();
st.erase(st.begin());
ans.push_back('a' + it[2]);
dfs(it[1]);
ans.push_back('-');
}
}
int main() {
cin.tie(0)->sync_with_stdio(false);
int N; cin >> N;
for (int i = 0; i < N; ++i) {
string s; cin >> s;
add_string(s);
}
dfs(0);
while (!ans.empty() && ans.back() == '-') ans.pop_back();
cout << ans.size() << endl;
for (auto it : ans) cout << it << '\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... |