#include <bits/stdc++.h>
#define MAX_N 25000
#define MAX_LEN 20
using namespace std;
int lcp[MAX_N], len[MAX_N];
string s[MAX_N];
vector <char> sol;
int main() {
int n, minLen, first, last, i, j, l;
bool cresc;
cin >> n;
for ( i = 0; i < n; i++ ) {
cin >> s[i];
len[i] = (int)s[i].size();
}
sort( s, s + n );
for ( i = 0; i < n; i++ ) {
j = (i + 1) % n;
l = 0;
while ( l < len[i] && l < len[j] && s[i][l] == s[j][l] )
l++;
lcp[i] = l;
}
minLen = MAX_LEN + 1;
last = -1;
cresc = true;
for ( i = 0; i < n; i++ ) {
if ( 2 * lcp[i] - len[i] < minLen ) {
minLen = 2 * lcp[i] - len[i];
last = i;
cresc = true;
}
if ( 2 * lcp[(i - 1 + n) % n] - len[i] < minLen ) {
minLen = 2 * lcp[(i - 1 + n) % n] - len[i];
last = i;
cresc = false;
}
}
if ( cresc ) {
first = (last + 1) % n;
l = 0;
for ( int k = 0; k < n; k++ ) {
i = (first + k) % n;
j = (i - 1 + n) % n;
while ( l > lcp[j] ) {
sol.push_back( '-' );
l--;
}
while ( l < len[i] ) {
sol.push_back( s[i][l] );
l++;
}
sol.push_back( 'P' );
}
} else {
first = (last - 1 + n) % n;
l = 0;
for ( int k = 0; k < n; k++ ) {
i = (first - k + n) % n;
j = (i - 1 + n) % n;
while ( l > lcp[j] ) {
sol.push_back( '-' );
l--;
}
while ( l < len[i] ) {
sol.push_back( s[i][l] );
l++;
}
sol.push_back( 'P' );
}
}
cout << sol.size() << "\n";
for ( char o: sol )
cout << o << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
980 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
980 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
980 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1040 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
1108 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
1280 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
1656 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
2468 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
2112 KB |
Line " |
2 |
Halted |
0 ms |
0 KB |
- |