// UUID: 4cce61c1-db47-4627-8e39-9d46edf2b03e
#include <iostream>
#include <vector>
using namespace std;
struct node {
vector <int> v;
bool endpoint = false;
int last;
node() {
v.assign(26, 0);
endpoint = false;
last = -1;
}
};
int n;
vector <string> w(3e4);
vector <node> t(1, node());
string ans;
void add(string s, bool l) {
int x = 0, i;
for (char c : s) {
i = c - 'a';
if (!t[x].v[i]) {
t[x].v[i] = t.size();
t.push_back(node());
}
if (l) t[x].last = i;
x = t[x].v[i];
}
t[x].endpoint = true;
}
void dfs(int x) {
if (t[x].endpoint) ans.push_back('P');
for (int i = 0; i < 26; i++) {
if (i == t[x].last || !t[x].v[i]) continue;
ans.push_back('a' + i);
dfs(t[x].v[i]);
ans.push_back('-');
}
if (t[x].last > -1) {
int i = t[x].last;
ans.push_back('a' + i);
dfs(t[x].v[i]);
ans.push_back('-');
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int longest = 0;
for (int i = 0; i < n; i++) {
cin >> w[i];
if (w[i].size() > w[longest].size()) longest = i;
}
for (int i = 0; i < n; i++) add(w[i], i == longest);
dfs(0);
while (ans.back() == '-') ans.pop_back();
cout << ans.size() << "\n";
for (char c : ans) cout << c << "\n";
}
| # | 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... |