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;
#define ll long long
#define pi pair<int, int>
#define fi first
#define se second
const int mxN = 1e5+5;
const int mod = 1e9+7;
const ll inf = 1e18;
struct node {
bool word;
int depth;
node *children[26];
node() {
word = 0, depth = 0;
for(int i = 0; i < 26; i++) children[i] = nullptr;
}
}; node *root = new node();
int n;
string s[mxN];
vector<char> ans;
// insert t
void add(string t) {
int len = t.length();
node *cur = root;
for(int i = 0; i < len; i++) {
cur->depth = max(cur->depth, len);
if(cur->children[t[i]-'a'] == nullptr) cur->children[t[i]-'a'] = new node();
cur = cur->children[t[i]-'a'];
}
cur->depth = max(cur->depth, len);
cur->word = 1;
}
// dfs in a trie starting from the root
void dfs(node *u, int back) {
if(u->word) ans.push_back('P'); // red vertice - end of the word
int p = 0;
node *x = nullptr;
for(int i = 0; i < 26; i++) {
node *v = u->children[i];
if(v == nullptr) continue;
if(x == nullptr || v->depth > x->depth) { x = v; p = i; }
}
for(int i = 0; i < 26; i++) {
node *v = u->children[i];
if(v == x || v == nullptr) continue;
ans.push_back(i+'a');
dfs(v, 0);
}
if(x != nullptr) { ans.push_back(p+'a'); dfs(x, back); }
if(!back) ans.push_back('-');
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++) {
cin >> s[i];
add(s[i]);
}
dfs(root, 1);
cout << ans.size() << "\n";
for(char i : ans) cout << 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... |