//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;
int origsize;
vector<char> instructions;
struct Trie {
unordered_map<char,Trie*> children;
int ultimo = 0,star = 0,deepshit = 0;
void insert(string* ss) {
ultimo++;
if (ss->empty()) {
star++;
deepshit = max(deepshit,origsize);
return;
}
char cc = ss->back();
ss->pop_back();
if (!children.count(cc)) children[cc] = new Trie;
children[cc]->insert(ss);
deepshit = max(deepshit,children[cc]->deepshit);
}
void dfs() {
while (star) {
star--;
instructions.push_back('P');
}
vector<pair<int,char>> childs;
for (auto it : children) childs.push_back({it.ss->deepshit,it.ff});
sort(childs.begin(),childs.end());
for (auto it : childs) {
instructions.push_back(it.ss);
children[it.ss]->dfs();
}
instructions.push_back('-');
}
} *root = new Trie;
void solve() {
int n;
cin >> n;
for (int i=1;i<=n;i++) {
cin >> S;
origsize = S.size();
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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1884 KB |
Output is correct |
2 |
Correct |
4 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5724 KB |
Output is correct |
2 |
Correct |
21 ms |
12008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
14032 KB |
Output is correct |
2 |
Correct |
12 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
35016 KB |
Output is correct |
2 |
Correct |
156 ms |
81580 KB |
Output is correct |
3 |
Correct |
84 ms |
40820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
26316 KB |
Output is correct |
2 |
Correct |
177 ms |
96976 KB |
Output is correct |
3 |
Correct |
90 ms |
46540 KB |
Output is correct |
4 |
Correct |
151 ms |
91336 KB |
Output is correct |