#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x.size())
using namespace std;
int commonPrefix(string x, string y) {
for (int i = 0; i < min(sz(x), sz(y)); ++i){
if (x[i] != y[i]) return i;
}
return min(sz(x), sz(y));
}
void solve() {
vector<char> ans;
int n, ml = 0, lcp;
cin >> n;
int l = 0, r = n-1;
string s[n], ms, last = "";
for (int i = 0; i < n; ++i) {
cin >> s[i];
if (sz(s[i]) > ml) {
ms = s[i];
ml = sz(s[i]);
}
}
sort(s, s+n);
for (int i = 0; i <= ml; ++i) {
while (commonPrefix(s[l], ms) == i) {
lcp = commonPrefix(last, s[l]);
for (int j = 0; j < (sz(last)-lcp); ++j) {
// cout << "-\n";
ans.push_back('-');
}
for (int j = lcp; j < sz(s[l]); ++j) {
// cout << s[l][j] << "\n";
ans.push_back(s[l][j]);
}
// cout << "P\n";
ans.push_back('P');
last = s[l];
++l;
}
while (commonPrefix(s[r], ms) == i) {
lcp = commonPrefix(last, s[r]);
for (int j = 0; j < (sz(last)-lcp); ++j) {
// cout << "-\n";
ans.push_back('-');
}
for (int j = lcp; j < sz(s[r]); ++j) {
// cout << s[r][j] << "\n";
ans.push_back(s[r][j]);
}
// cout << "P\n";
ans.push_back('P');
last = s[r];
--r;
}
}
cout << (sz(ans)-1) << "\n";
for (int i = 0; i < (sz(ans)-1); ++i) {
cout << ans[i] << "\n";
}
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
720 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Runtime error |
4 ms |
576 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
596 KB |
Output is correct |
2 |
Correct |
7 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1240 KB |
Output is correct |
2 |
Correct |
7 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
2452 KB |
Output is correct |
2 |
Correct |
36 ms |
4688 KB |
Output is correct |
3 |
Correct |
37 ms |
3756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
2260 KB |
Output is correct |
2 |
Correct |
51 ms |
5612 KB |
Output is correct |
3 |
Correct |
26 ms |
4224 KB |
Output is correct |
4 |
Runtime error |
13 ms |
6812 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |