#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int n,mx;
string s,larger;
vector<char> kq;
struct node{
node* child[26];
bool fl,e;
node(){
fo(i,0,25) child[i] = nullptr;
e = fl = false;
}
};
node* root;
struct Trie{
void Insert(string s, bool o){
node* p = root;
for(char a : s){
if(!p -> child[a -'a']) p -> child[a - 'a'] = new node;
p = p -> child[a - 'a'];
p -> fl = o;
}
p -> fl = o;
p -> e = true;
}
void Dfs(node *p){
if(p -> e) kq.push_back('P');
int tmp = -1;
fo(i,0,25)
if(p -> child[i]) {
node* v = p -> child[i];
if(v -> fl){
tmp = i;
continue;
}
kq.push_back('a' + i);
Dfs(v);
kq.push_back('-');
}
if(tmp != -1){
kq.push_back('a' + tmp);
Dfs(p -> child[tmp]);
}
}
} trie;
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
root = new node;
cin >> n;
fo(i,1,n) {
cin >> s;
trie.Insert(s,0);
if(s.size() > mx){
mx = s.size();
larger = s;
}
}
trie.Insert(larger,1);
trie.Dfs(root);
cout << kq.size()<<"\n";
for(auto k : kq) cout << k<<"\n";
}
Compilation message
printer.cpp: In function 'int main()':
printer.cpp:60:21: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | if(s.size() > mx){
| ~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1740 KB |
Output is correct |
2 |
Correct |
3 ms |
2252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5960 KB |
Output is correct |
2 |
Correct |
19 ms |
12600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
14796 KB |
Output is correct |
2 |
Correct |
9 ms |
3300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
36676 KB |
Output is correct |
2 |
Correct |
115 ms |
83636 KB |
Output is correct |
3 |
Correct |
63 ms |
43200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
28608 KB |
Output is correct |
2 |
Correct |
143 ms |
99572 KB |
Output is correct |
3 |
Correct |
71 ms |
49000 KB |
Output is correct |
4 |
Correct |
116 ms |
93984 KB |
Output is correct |