#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 5e5+10;
int trie[mxN][26];
int cnt = 0;
bool stop[mxN], marked[mxN];
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;
}
vector<char> ans;
void mark(string s) {
int node = 0;
for(auto c : s) {
marked[trie[node][c-'a']] = true;
node = trie[node][c-'a'];
}
}
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 longest = "";
for(int i = 0; i < n; i++) {
string s;
cin >> s;
insert(s);
if(s.length() > longest.length()) longest = s;
}
mark(longest);
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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 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 |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3160 KB |
Output is correct |
2 |
Correct |
12 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 |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
17904 KB |
Output is correct |
2 |
Correct |
71 ms |
40652 KB |
Output is correct |
3 |
Correct |
37 ms |
20948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
13972 KB |
Output is correct |
2 |
Correct |
81 ms |
48588 KB |
Output is correct |
3 |
Correct |
45 ms |
24128 KB |
Output is correct |
4 |
Correct |
70 ms |
45880 KB |
Output is correct |