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;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 5e5;
struct node {
char c;
int v = 0, end = 0;
node * child[26];
node() {
for (int i = 0; i < 26; i++) {
child[i] = NULL;
}
}
};
string ans;
node *root = new node;
void insert(string s) {
node *cur = root;
for (auto x : s) {
int ind = x - 'a';
if (cur -> child[ind] == NULL) {
cur -> child[ind] = new node;
cur -> child[ind] -> c = x;
}
cur = cur -> child[ind];
}
cur -> end++;
}
void reverse(string s) {
node *cur = root;
for (auto x : s) {
int ind = x - 'a';
swap(cur -> child[25], cur -> child[ind]);
cur = cur -> child[25];
}
}
void dfs(node *cur) {
for (int i = 0; i < cur -> end; i++)
ans += "P";
for (int i = 0; i < 26; i++) {
if (cur -> child[i] == NULL)
continue;
ans += (cur -> child[i] -> c);
dfs(cur -> child[i]);
ans += "-";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s, mx;
for (int i = 0; i < n; i++) {
cin >> s;
if (len(s) > len(mx)) {
mx = s;
}
insert(s);
}
reverse(mx);
dfs(root);
while (ans.back() == '-')
ans.pop_back();
cout << len(ans) << '\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... |