#include "bits/stdc++.h"
using namespace std;
#define int long long
const int sz = 64 * 2e5 + 5;
const int inf = 1e18;
int trie[sz][26];
int depth[sz];
int leaf[sz];
int nxt = 0, cnt = 0;
char ch[sz];
vector<int> adj[sz];
vector<char> res;
int n;
struct Trie
{
void add(string s)
{
int root = 0;
for(int i = 0; i < s.size(); i++)
{
int child = s[i] - 'a';
if (!trie[root][child])
{
trie[root][child] = ++nxt;
adj[root].push_back(trie[root][child]);
ch[trie[root][child]] = s[i];
}
root = trie[root][child];
depth[root] = max(depth[root], (int)s.size() - i);
}
leaf[root] = true;
}
void dfs(int node)
{
if (node) res.push_back(ch[node]);
if (leaf[node]) res.push_back('P'), cnt++;
for(int to : adj[node]) dfs(to);
if (cnt != n) res.push_back('-');
}
};
void solve()
{
cin >> n;
Trie tree;
for(int i = 1; i <= n; i++)
{
string s;
cin >> s;
tree.add(s);
}
for(int i = 0; i <= nxt; i++) sort(adj[i].begin(), adj[i].end(), [&](int a, int b)
{
return depth[a] < depth[b];
});
tree.dfs(0);
cout << res.size() << endl;
for(char c : res) cout << c << endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++) solve();
}
Compilation message
printer.cpp: In member function 'void Trie::add(std::string)':
printer.cpp:24:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
/tmp/ccGDNDST.o: in function `solve()':
printer.cpp:(.text+0xcc0): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
printer.cpp:(.text+0xd2d): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
printer.cpp:(.text+0x1001): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cout' defined in .bss._ZSt4cout section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
printer.cpp:(.text+0x10a0): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cout' defined in .bss._ZSt4cout section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
/tmp/ccGDNDST.o: in function `main':
printer.cpp:(.text.startup+0x12): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
printer.cpp:(.text.startup+0x1d): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cout' defined in .bss._ZSt4cout section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
/tmp/ccGDNDST.o: in function `_GLOBAL__sub_I_trie':
printer.cpp:(.text.startup+0x4b): relocation truncated to fit: R_X86_64_PC32 against `.bss'
printer.cpp:(.text.startup+0x65): relocation truncated to fit: R_X86_64_PC32 against `.bss'
/usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(vterminate.o): in function `__gnu_cxx::__verbose_terminate_handler()':
(.text._ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x1e): relocation truncated to fit: R_X86_64_PC32 against `.bss._ZZN9__gnu_cxx27__verbose_terminate_handlerEvE11terminating'
(.text._ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x2b): relocation truncated to fit: R_X86_64_PC32 against `.bss._ZZN9__gnu_cxx27__verbose_terminate_handlerEvE11terminating'
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status