#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));
for(auto i : v){
ans.pb(char('a' + i.F.S));
get(i.S);
}
if(leaf[node]) ans.pb('P');
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 |
8 ms |
21580 KB |
Output is correct |
2 |
Correct |
9 ms |
21572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
21628 KB |
Output is correct |
2 |
Correct |
9 ms |
21580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
21648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
21528 KB |
Output is correct |
2 |
Incorrect |
9 ms |
21580 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
21580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
21708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
21828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
22276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
22924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
22748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |