#include <bits/stdc++.h>
using namespace std;
// Hi
int n;
vector<vector<int>> Trie;
vector<bool> last,marked;
vector<int> vv;
void add(string s){
int curr = 0;
for(auto x:s){
int c = x-'a';
if(Trie[curr][c] == -1){
Trie[curr][c] = Trie.size();
Trie.push_back(vv);
last.push_back(0);
marked.push_back(0);
}
curr = Trie[curr][c];
}
last[curr] = 1;
}
void mark(string s){
int curr = 0;
for(auto x:s){
int c = x-'a';
marked[Trie[curr][c]] = 1;
curr = Trie[curr][c];
}
}
vector<char> ops;
void dfs(int curr){
vector<int> left;
for(int c=0;c<26;c++){
if(Trie[curr][c] == -1)continue;
if(marked[Trie[curr][c]])left.push_back(c);
else {
ops.push_back(c+'a');
dfs(Trie[curr][c]);
ops.push_back('-');
}
}
for(auto c:left){
if(last[curr])ops.push_back('P');
ops.push_back(c+'a');
dfs(Trie[curr][c]);
}
if(last[curr] && !marked[curr])ops.push_back('P');
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
for(int i=0;i<26;i++)vv.push_back(-1);
cin>>n;
string w;
string longest = "";
Trie.push_back(vv);
last.push_back(0);
marked.push_back(0);
for(int i=0;i<n;i++){
cin>>w;
add(w);
if(w.size() > longest.size()){
longest = w;
}
}
mark(longest); // mark the longest word to keep the last
dfs(0);
cout << ops.size() +1 <<'\n';
for(auto c:ops)cout<<c<<'\n';
cout<<'P'<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1228 KB |
Output is correct |
2 |
Correct |
4 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
3812 KB |
Output is correct |
2 |
Correct |
23 ms |
7936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
9392 KB |
Output is correct |
2 |
Correct |
10 ms |
2312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
23128 KB |
Output is correct |
2 |
Correct |
158 ms |
52820 KB |
Output is correct |
3 |
Correct |
90 ms |
27220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
18112 KB |
Output is correct |
2 |
Correct |
193 ms |
62648 KB |
Output is correct |
3 |
Correct |
99 ms |
30800 KB |
Output is correct |
4 |
Correct |
175 ms |
59096 KB |
Output is correct |