This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;
int n;
string s;
struct TTrie{
TTrie* adj[26];
TTrie(){
for (int i = 0; i <= 25; ++i) adj[i] = nullptr;
}
bool p = 0;
int max_depth = 0;
};
TTrie* root = new TTrie();
int cnt = 0;
void add(TTrie* id, int i = 0, int m = s.length()){
if (i == m){
if (id->p) cerr << "?";
id->p = 1;
id->max_depth = max(id->max_depth, m);
return;
}
if (!id->adj[s[i] - 'a']) id->adj[s[i] - 'a'] = new TTrie();
add(id->adj[s[i] - 'a'], i + 1, m);
id->max_depth = max(id->max_depth, m);
}
vector <char> v;
void dfs(TTrie* id = root, bool t = 1){
TTrie* tem = nullptr;
int p = -1;
if (id->p) v.pb('P');
for (int i = 0; i <= 25; ++i){
if (id->adj[i]){
if (id->adj[i]->max_depth == root->max_depth && t){
tem = id->adj[i];
p = i;
}
}
}
for (int i = 0; i <= 25; ++i){
if (id->adj[i]){
if (i == p) continue;
v.pb(i + 'a');
dfs(id->adj[i], 0);
v.pb('-');
}
}
if (tem){
v.pb(p + 'a');
dfs(tem, 1);
}
}
void Init(){
cin >> n;
int tem = 0;
for (int i = 1; i <= n; ++i){
cin >> s;
tem = max(tem, s.length());
add(root);
}
dfs();
cout << v.size() << "\n";
for (char c: v) cout << c << "\n";
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message (stderr)
printer.cpp: In function 'int main()':
printer.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |