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 int long long
#define f first
#define s second
vector<char> ans;
const int maxn = 5e5 + 69;
int trie[maxn][26], kraj[maxn], ban[maxn];
int n, node;
void add(string s) {
int m = s.size();
int t = 0;
for (int i = 0;i < m;i++) {
int x = s[i] - 'a';
if (trie[t][x] == 0)
trie[t][x] = ++node;
t = trie[t][x];
}
kraj[t]++;
}
void dfs(int v) {
for (int i = 0;i < kraj[v];i++)
ans.push_back('P');
int x = -1;
for (int i = 0;i < 26;i++) {
if (trie[v][i] && ban[trie[v][i]] != 1) {
ans.push_back((char)(i + 'a'));
dfs(trie[v][i]);
ans.push_back('-');
}
else if (ban[trie[v][i]] == 1)
x = i;
}
if (x != -1) {
ans.push_back((char)(x + 'a'));
dfs(trie[v][x]);
ans.push_back('-');
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n;
vector<string> a(n);
string mx;
for (int i = 0;i < n;i++) {
cin >> a[i];
if ((int)a[i].size() > (int)mx.size())
mx = a[i];
add(a[i]);
}
int t = 0;
for (int i = 0;i < (int)mx.size();i++) {
int x = mx[i] - 'a';
t = trie[t][x];
ban[t] = 1;
}
dfs(0);
while (ans.back() == '-')
ans.pop_back();
cout << ans.size() << '\n';
for (auto x : ans)
cout << x << '\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... |