#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
int n, m, c[500001][26];
bool l[500001];
string ans, b;
void dfs(int u = 0) {
if (l[u])
ans.push_back('P');
for (int i = 0; i < 26; ++i)
if (c[u][i]) {
ans.push_back('a' + i);
dfs(c[u][i]);
ans.push_back('-');
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
m = 1;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
if (s.size() > b.size())
b = s;
int u = 0;
for (char d : s) {
if (!c[u][d - 'a'])
c[u][d - 'a'] = m++;
u = c[u][d - 'a'];
}
l[u] = 1;
}
int u = 0;
for (char d : b) {
for (int i = 0; i < 26; ++i)
if (c[u][i] && i != d - 'a') {
ans.push_back('a' + i);
dfs(c[u][i]);
ans.push_back('-');
}
ans.push_back(d);
u = c[u][d - 'a'];
if (l[u])
ans += 'P';
}
cout << ans.size() << "\n";
for (char d : ans)
cout << d << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1132 KB |
Output is correct |
2 |
Correct |
4 ms |
1388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
3180 KB |
Output is correct |
2 |
Correct |
17 ms |
6380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
7420 KB |
Output is correct |
2 |
Correct |
8 ms |
2028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
17928 KB |
Output is correct |
2 |
Correct |
104 ms |
40996 KB |
Output is correct |
3 |
Correct |
58 ms |
21512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
14088 KB |
Output is correct |
2 |
Correct |
126 ms |
48676 KB |
Output is correct |
3 |
Correct |
65 ms |
24200 KB |
Output is correct |
4 |
Correct |
110 ms |
46000 KB |
Output is correct |