#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector <string> s(n);
for ( auto &x: s ) cin >> x;
sort(all(s));
auto f = [&](auto &u, auto &v){
int i = 0;
for (; i < min((int)u.size(), (int)v.size()); i++ ){
if ( u[i] != v[i] ) break;
}
return i;
};
vector <int> pf(n);
for ( int i = 1; i < n; i++ ){
pf[i] = pf[i - 1] + f(s[i], s[i - 1]);
}
auto q = [&](int i){
return pf[i] + (i + 1 < n ? f(s[0], s[i + 1]) + pf[n - 1] - pf[i + 1] : 0);
};
auto p = [&](int i){
return pf[n - 1] - pf[i] + (i > 0 ? f(s[n - 1], s[i - 1]) + pf[i - 1] : 0);
};
int opt = 0, j = 0;
for ( int i = 0; i < n; i++ ){
int t = max(q(i), p(i));
t += s[i].size();
if ( chmax(opt, t) ){
j = i;
}
}
vector <string> a;
if ( q(j) >= p(j) ){
for ( int i = j; i >= 0; i-- ){
a.pb(s[i]);
}
for ( int i = j + 1; i < n; i++ ){
a.pb(s[i]);
}
} else{
for ( int i = j; i < n; i++ ){
a.pb(s[i]);
}
for ( int i = j - 1; i >= 0; i-- ){
a.pb(s[i]);
}
}
reverse(all(a));
for ( auto &x: a ) cout << x << ln;
vector <char> op;
for ( int i = 0; i < n; i++ ){
if ( i > 0 ){
int t = f(a[i - 1], a[i]), d = (int)a[i - 1].size() - t;
while ( d-- ){
op.pb('-');
}
for ( int j = t; j < a[i].size(); j++ ){
op.pb(a[i][j]);
}
} else{
for ( auto &x: a[i] ){
op.pb(x);
}
} op.pb('P');
}
cout << op.size() << ln;
for ( auto &x: op ){
cout << x << ln;
}
cout << '\n';
}
Compilation message
printer.cpp: In function 'int main()':
printer.cpp:89:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for ( int j = t; j < a[i].size(); j++ ){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Expected integer, but "tptttykduyvxjbzhqup" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Expected integer, but "yerx" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Expected integer, but "xrmqggqby" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Expected integer, but "xp" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Expected integer, but "zyyauyvyazz" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
856 KB |
Expected integer, but "zzo" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
1964 KB |
Expected integer, but "zzzzwjpobpgilitk" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
4036 KB |
Expected integer, but "zzygnldzidmxohgd" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
3796 KB |
Expected integer, but "zzzfgmldsz" found |
2 |
Halted |
0 ms |
0 KB |
- |