//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 |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
1880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
5720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
13788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
83 ms |
34708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
69 ms |
26068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |