// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int cnt_s = 26;
struct node {
bool val;
int lnk[cnt_s];
node() {
val = false;
for (int i = 0; i < cnt_s; i++) {
lnk[i] = -1;
}
}
};
vector<node> trie(1);
void add(string& s) {
int v = 0;
for (char c : s) {
int u = trie[v].lnk[c - 'a'];
if (u == -1) {
u = trie.size();
trie.emplace_back();
trie[v].lnk[c - 'a'] = u;
}
v = u;
}
trie[v].val = true;
}
vector<char> res;
string s1;
int j = 0;
bool is_ob = true;
void get_way(int v) {
if (is_ob) {
if (j >= (int)s1.size()) {
res.push_back('P');
is_ob = false;
return;
}
int u = trie[v].lnk[s1[j] - 'a'];
trie[v].lnk[s1[j++] - 'a'] = -1;
get_way(u);
res.push_back(s1[--j]);
}
if (trie[v].val) {
res.push_back('P');
}
for (int i = 0; i < cnt_s; i++) {
int el = trie[v].lnk[i];
if (el != -1) {
res.push_back('-');
get_way(el);
res.push_back('a' + i);
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
// freopen("input.txt", "r", stdin);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
add(s);
if (s.size() > s1.size()) {
s1 = s;
}
}
get_way(0);
reverse(res.begin(), res.end());
cout << res.size() << "\n";
for (char el : res) {
cout << el << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
1028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1316 KB |
Output is correct |
2 |
Correct |
3 ms |
2164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5392 KB |
Output is correct |
2 |
Correct |
10 ms |
9036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8012 KB |
Output is correct |
2 |
Correct |
6 ms |
2420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
28612 KB |
Output is correct |
2 |
Correct |
76 ms |
57780 KB |
Output is correct |
3 |
Correct |
40 ms |
29636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
16068 KB |
Output is correct |
2 |
Correct |
82 ms |
57804 KB |
Output is correct |
3 |
Correct |
43 ms |
29632 KB |
Output is correct |
4 |
Correct |
73 ms |
57536 KB |
Output is correct |