// UUID: 297c6817-13b8-4b88-90aa-49301e090228
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int nxt[26];
int last = -1;
Node(){
fill(nxt, nxt+26, -1);
}
};
vector <bool> ending(5e5 + 5, false);
vector <Node> trie(1);
void Insert(string s)
{
int u = 0;
for(auto x : s){
int c = x - 'a';
if(trie[u].nxt[c] == -1){
trie[u].nxt[c] = trie.size();
Node newnode;
trie.push_back(newnode);
}
u = trie[u].nxt[c];
}
ending[u] = true;
}
string ans = "";
void dfs(int u)
{
if(ending[u]) ans += 'P';
int c;
for(int i = 0; i < 26; i++){
if(trie[u].nxt[i] == trie[u].last){
c = i;
continue;
}
if(trie[u].nxt[i] != -1){
ans += i + 'a';
dfs(trie[u].nxt[i]);
}
}
if(trie[u].last != -1){
ans += c + 'a';
dfs(trie[u].last);
}
ans += '-';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
string longest = "";
for(int i = 0; i < n; i++){
string s; cin >> s;
Insert(s);
if(s.size() > longest.size()) longest = s;
}
int u = 0;
for(auto x : longest){
int c = x - 'a';
trie[u].last = trie[u].nxt[c];
u = trie[u].nxt[c];
}
dfs(0);
while(!ans.empty() && 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... |