#include"bits/stdc++.h"
#define f(i,j,k) for(int i=j;i<=k;i++)
#define pb push_back
const int N = 3e4;
using namespace std;
int n;
string s, last;
vector <char> ans;
struct Node{
int ed;
Node* child[30];
Node(){
ed = 0;
f(i,0,29) child[i] = nullptr;
}
};
struct Trie{
Node *root;
Trie(){ root = new Node;}
void __up(string S){
Node *it = root;
for(auto ii : S){
int j = (int) ii - 'a';
if(!it->child[j]) it->child[j] = new Node;
it = it->child[j];
}
it->ed = 1;
}
void dfs(Node* i1, int len, int ok){
int Alast = ok ? (int) last[len] - 'a' : -1;
if(i1->ed == 1) ans.pb('P');
f(i,0,25){
if(i == Alast || !i1->child[i]) continue;
ans.pb('a'+i);
dfs(i1->child[i],len+1,0);
ans.pb('-');
}
if(ok){
ans.pb(last[len]);
if(len == last.size() - 1) ans.pb('P');
else dfs(i1->child[Alast],len+1,1);
}
}
}lxv;
main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
f(i,1,n){
cin>>s;
lxv.__up(s);
if(s.size() > last.size()) last = s;
}
lxv.dfs(lxv.root,0,1);
cout<<ans.size()<<'\n';
for(auto i: ans) cout<<i<<'\n';
}
Compilation message
printer.cpp: In member function 'void Trie::dfs(Node*, int, int)':
printer.cpp:43:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if(len == last.size() - 1) ans.pb('P');
| ~~~~^~~~~~~~~~~~~~~~~~
printer.cpp: At global scope:
printer.cpp:51:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
51 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1984 KB |
Output is correct |
2 |
Correct |
3 ms |
2508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6724 KB |
Output is correct |
2 |
Correct |
17 ms |
14144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
16696 KB |
Output is correct |
2 |
Correct |
8 ms |
3776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
41756 KB |
Output is correct |
2 |
Correct |
124 ms |
95244 KB |
Output is correct |
3 |
Correct |
59 ms |
49060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
32556 KB |
Output is correct |
2 |
Correct |
128 ms |
113252 KB |
Output is correct |
3 |
Correct |
75 ms |
55732 KB |
Output is correct |
4 |
Correct |
118 ms |
106864 KB |
Output is correct |