This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Dost SEFEROĞLU
#pragma GCC optimize("O3,unroll-loops,Ofast")
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define vi vector<int>
const int N = 5e5+100,inf = 1e9,MOD = 1e9+7;
string S;
vector<char> instructions;
struct Trie {
unordered_map<char,Trie*> children;
int ultimo = 0,star = 0;
void insert(string* ss) {
ultimo++;
if (ss->empty()) {
star++;
return;
}
char cc = ss->back();
ss->pop_back();
if (!children.count(cc)) children[cc] = new Trie;
children[cc]->insert(ss);
}
void dfs() {
while (star) {
star--;
instructions.push_back('P');
}
for (auto it : children) {
if (it.ss->ultimo) {
instructions.push_back(it.ff);
it.ss->dfs();
}
}
instructions.push_back('-');
}
} *root = new Trie;
void solve() {
int n;
cin >> n;
for (int i=1;i<=n;i++) {
cin >> S;
reverse(S.begin(),S.end());
root->insert(&S);
}
root->dfs();
while (instructions.back() == '-') instructions.pop_back();
cout << instructions.size() << '\n';
for (auto it : instructions) cout << it << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# | 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... |