// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#define _GLIBCXX_FILESYSTEM
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 5e5+2;
int sz[N];
struct Trie{
struct node{
int nxt[26];
bool end;
node(){
memset(nxt,-1,sizeof(nxt));
end = 0;
}
};
vector<node> trie;
Trie(){
trie.emplace_back();
}
void insert(string &s){
int cur = 0;
for(int i = 0; i < s.size(); i++){
if(trie[cur].nxt[s[i]-'a'] == -1){
trie[cur].nxt[s[i]-'a'] = trie.size();
trie.push_back(node());
}
cur = trie[cur].nxt[s[i]-'a'];
}
trie[cur].end = 1;
}
};
Trie trie;
void dfs_sz(int cur){
for(int i = 0; i < 26; i++){
int v = trie.trie[cur].nxt[i];
if(v != -1){
dfs_sz(v);
sz[cur] = max(sz[cur],sz[v]);
}
}
sz[cur]++;
}
vector<char> ans;
void dfs(int cur,bool rakhbo = 1){
bool skip = 0;
int bigChild = -1;
for(int i = 0; i < 26; i++){
int v = trie.trie[cur].nxt[i];
if(v == -1) continue;
// cerr << cur << ' ' << v << ' ' << sz[v] << '\n';
if(sz[v] == sz[cur]-1 and !skip){
skip = 1;
bigChild = i;
continue;
}
ans.push_back((char)('a'+i));
dfs(v,0);
}
if(~bigChild){
int v = trie.trie[cur].nxt[bigChild];
ans.push_back((char)('a'+bigChild));
dfs(v,(1&rakhbo));
}
if(trie.trie[cur].end) ans.push_back('P');
if(!rakhbo) ans.push_back('-');
}
void solve() {
int n;
cin >> n;
for(int i = 1; i <= n; i++){
string s;
cin >> s;
trie.insert(s);
}
dfs_sz(0);
dfs(0);
cout << ans.size() << '\n';
for(auto x: ans) cout << x << '\n';
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
int tc = 1;
// cin >> tc;
for(int kase = 1; kase <= tc; kase++) {
//cout << "Case " << kase << ": ";
solve();
}
return 0;
}
Compilation message
printer.cpp: In member function 'void Trie::insert(std::string&)':
printer.cpp:26:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i = 0; i < s.size(); i++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
464 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
printed invalid word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
604 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
1320 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5648 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
9288 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
28628 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
16580 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |