#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <utility>
#include <cmath>
#include <numeric>
#include <assert.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 200007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
vector <char> ans;
int n;
struct Trie{
int to[M][26] = {0}, mx[M] = {0}, sz;
bool leaf[M] = {0};
void add(string s){
int cur = 0;
mx[0] = max(mx[0], (int)s.size());
for(int i = 0; i < s.size(); ++i){
int c = s[i] - 'a';
if(!to[cur][c]) to[cur][c] = ++sz;
cur = to[cur][c];
mx[cur] = max(mx[cur], (int)s.size());
}
leaf[cur] = 1;
return;
}
void get(int node){
vector < pair < pair <int,int>, int > > v;
for(int i = 0; i < 26; ++i) if(to[node][i]) v.pb({{mx[to[node][i]], i}, to[node][i]});
sort(all(v));
if(leaf[node]) ans.pb('P');
for(auto i : v){
ans.pb(char('a' + i.F.S));
get(i.S);
}
ans.pb('-');
}
}T;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
string s;
cin >> s;
T.add(s);
}
T.get(0);
while(ans.back() == '-') ans.pop_back();
cout << ans.size() << endl;
for(auto i : ans) cout << i << endl;
return 0;
}
Compilation message
printer.cpp: In member function 'void Trie::add(std::string)':
printer.cpp:37:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i = 0; i < s.size(); ++i){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21576 KB |
Output is correct |
2 |
Correct |
9 ms |
21580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
21580 KB |
Output is correct |
2 |
Correct |
10 ms |
21580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
21580 KB |
Output is correct |
2 |
Correct |
11 ms |
21644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21620 KB |
Output is correct |
2 |
Correct |
9 ms |
21588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
21580 KB |
Output is correct |
2 |
Correct |
9 ms |
21648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21708 KB |
Output is correct |
2 |
Correct |
11 ms |
21636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
21836 KB |
Output is correct |
2 |
Correct |
19 ms |
22084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
22096 KB |
Output is correct |
2 |
Correct |
14 ms |
21956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
22832 KB |
Output is correct |
2 |
Runtime error |
32 ms |
44180 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
22524 KB |
Output is correct |
2 |
Runtime error |
31 ms |
44060 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |