#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 5e5+10;
int trie[mxN][26];
bool stop[mxN], marked[mxN];
int cnt = 0;
string alpha = "abcdefghijklmnopqrstuvwxyz";
void insert(string s) {
int node = 0;
for(auto c : s) {
if(trie[node][c-'a'] == 0) trie[node][c-'a'] = ++cnt;
node = trie[node][c-'a'];
}
stop[node] = true;
}
void mark(string s) {
int node = 0;
for(auto c : s) {
marked[trie[node][c-'a']] = true;
node = trie[node][c-'a'];
}
}
vector<char> ans;
void dfs(int node) {
if(stop[node]) ans.push_back('P');
vector<pair<int, char>> later;
for(auto c : alpha) {
int k = trie[node][c-'a'];
if(k && marked[k]) later.push_back({k, c});
else if(k) {
ans.push_back(c);
dfs(k);
ans.push_back('-');
}
}
for(auto it : later) {
ans.push_back(it.second);
dfs(it.first);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
string lng = "";
for(int i = 0; i < n; i++) {
string s;
cin >> s;
insert(s);
if(s.length() > lng.length()) lng = s;
}
mark(lng);
dfs(0);
cout << ans.size() << '\n';
for(auto it : ans) {
cout << it << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 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 |
0 ms |
344 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 |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3164 KB |
Output is correct |
2 |
Correct |
11 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
7392 KB |
Output is correct |
2 |
Correct |
5 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
17884 KB |
Output is correct |
2 |
Correct |
71 ms |
40648 KB |
Output is correct |
3 |
Correct |
37 ms |
20948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
14036 KB |
Output is correct |
2 |
Correct |
87 ms |
48036 KB |
Output is correct |
3 |
Correct |
42 ms |
23760 KB |
Output is correct |
4 |
Correct |
72 ms |
45508 KB |
Output is correct |