This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 1e9;
const int mod = 1e9+7;
const int maxn = 8e5 + 1;
int wt[maxn][26], mx[maxn], ed[maxn], cnt;
void init(){
for(int j = 0; j < 26; j++)wt[cnt][j] = -1;
cnt++;
}
void go(int v, string s, int it){
mx[v] = max(mx[v], (int)s.size());
if(it == (int)s.size()){
ed[v]++;
return;
}
int ch = s[it] - 'a';
if(wt[v][ch] == -1)wt[v][ch] = cnt, init();
go(wt[v][ch], s, it + 1);
}
vt<char> out;
void ans(int v, bool ok){
while(ed[v]--)out.pb('P');
if(ok){
int ma = -1, it = -1;
for(int j = 0; j < 26; j++){
if(wt[v][j] != -1){
if(ma == -1 || mx[v] == mx[wt[v][j]])ma = wt[v][j], it = j;
}
}
for(int j = 0; j < 26; j++){
if(wt[v][j] != -1 && j != it){
out.pb(char('a' + j));
ans(wt[v][j], 0);
out.pb('-');
}
}
if(it == -1)return;
out.pb(char('a' + it));
ans(wt[v][it], 1);
return;
}
for(int j = 0; j < 26; j++){
if(wt[v][j] != -1){
out.pb(char('a' + j));
ans(wt[v][j], 0);
out.pb('-');
}
}
}
void solve() {
int n;
cin >> n;
string s[n];
init();
for(int i = 0; i < n; i++){
cin >> s[i];
go(0, s[i], 0);
}
ans(0, 1);
cout << out.size() << '\n';
for(auto i : out)cout << i << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int times = 1;
//cin >> times;
for(int i = 1; i <= times; i++) {
solve();
}
return 0;
}
# | 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... |