// UUID: 5fc0b434-98ee-4d76-ba6e-9b400a1d1fcb
#include <bits/stdc++.h>
using namespace std;
struct abctree {
abctree* next[26];
bool end;
bool inthelongest;
abctree() {
for (int i = 0; i < 26; i++) next[i] = nullptr;
end = false;
inthelongest = false;
}
};
abctree* mainroot;
vector<char> out;
string longest = "";
void add(abctree* root, string s) {
abctree* cur = root;
for (char c:s) {
int index = c - 'a';
if (!cur -> next[index]) {
cur -> next[index] = new abctree;
}
cur = cur -> next[index];
}
cur -> end = true;
}
void print(abctree* root) {
abctree* cur = root;
for (int i = 0;i < 26;i++) {
if (cur -> next[i] and !cur -> next[i] -> inthelongest) {
out.push_back(char(i + 'a'));
print(cur -> next[i]);
}
}
for (int i = 0;i < 26;i++) {
if (cur -> next[i] and cur -> next[i] -> inthelongest) {
out.push_back(char(i + 'a'));
print(cur -> next[i]);
}
}
if (cur -> end) {
out.push_back('P');
}
if (mainroot != cur) {
out.push_back('-');
}
}
int main() {
int n;cin >> n;
mainroot = new abctree;
string s;
for (int i = 0;i < n;i++) {
cin >> s;
if (s.size() > longest.size()) longest = s;
add(mainroot, s);
}
abctree* cur = mainroot;
mainroot -> inthelongest = true;
for (char c:longest) {
cur = cur -> next[c - 'a'];
cur -> inthelongest = true;
}
print(mainroot);
cout << out.size() - longest.size() << endl;
for (int i = 0;i < out.size() - longest.size();i++) {
cout << out[i] << endl;
}
}
| # | 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... |