#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
VOI23 gold medal
*/
struct Node {
int h, next[26], cnt, pos;
Node() { h = 0; memset(next, 0, sizeof next); cnt = pos = 0; }
};
const int oo = 1e9 + 5;
const int M = 5e5 + 5;
const int N = 25e3 + 5;
int n, d[M][2], pre[M], root = 0, cnt = 0;
str s[N], ans;
Node trie[M];
vi p;
void add(int &cur, int h, str &s, int pos) {
if (!cur) {
cur = ++cnt;
trie[cnt].h = h;
}
++trie[cur].cnt;
trie[cur].pos = pos;
if (h >= sz(s)) return;
add(trie[cur].next[s[h] - 'a'], h + 1, s, pos);
}
int dp(int i, bool j) {
int &rs = d[i][j];
if (rs != -1) return rs;
if (trie[i].cnt == 1) {
int len = sz(s[trie[i].pos]);
if (!j) rs = 2 * (len - trie[i].h + 1);
else rs = len - trie[i].h + 1;
return rs;
}
if (!j) {
rs = 0;
FOR(z, 0, 25) {
int np = trie[i].next[z];
if (!np) continue;
int cnt = trie[np].cnt;
rs += cnt - 1 + dp(np, 0);
}
return rs;
}
rs = oo;
int cnt0 = 0, cnt1 = 0, tcnt = 0;
FOR(z, 0, 25) {
int np = trie[i].next[z];
if (!np) continue;
tcnt += trie[np].cnt - 1;
cnt0 += dp(np, 0);
cnt1 += dp(np, 1);
}
FOR(z, 0, 25) {
int np = trie[i].next[z];
if (!np) continue;
int ncnt0 = cnt0;
ncnt0 -= dp(np, 0);
rs = min(rs, dp(np, 1) + ncnt0 + tcnt);
}
return rs;
}
void trace(int i, bool j) {
if (trie[i].cnt == 1) {
p.pb(trie[i].pos);
return;
}
if (!j) {
FOR(z, 0, 25) {
int np = trie[i].next[z];
if (!np) continue;
trace(np, 0);
}
return;
}
int cnt0 = 0, cnt1 = 0, tcnt = 0;
FOR(z, 0, 25) {
int np = trie[i].next[z];
if (!np) continue;
tcnt += trie[np].cnt - 1;
cnt0 += dp(np, 0);
cnt1 += dp(np, 1);
}
int cur = dp(i, j), pos = -1;
FOR(z, 0, 25) {
int np = trie[i].next[z];
if (!np) continue;
int ncnt0 = cnt0;
ncnt0 -= dp(np, 0);
if (dp(np, 1) + ncnt0 + tcnt == cur) pos = z;
}
FOR(z, 0, 25) {
int np = trie[i].next[z];
if (!np || z == pos) continue;
trace(np, 0);
}
trace(trie[i].next[pos], 1);
}
int lcp(str &a, str &b) {
int len = 0;
FOR(i, 0, min(sz(a), sz(b)) - 1) {
if (a[i] != b[i]) break;
++len;
}
return len;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n;
FOR(i, 1, n) cin >> s[i];
FOR(i, 1, n) add(root, 0, s[i], i);
memset(d, -1, sizeof d);
cout << dp(1, 1) + n << '\n';
trace(1, 1);
EACH(i, s[p[0]]) ans.pb(i);
ans.pb('P');
FOR(i, 1, n - 1) {
int pre = p[i - 1], cur = p[i], len = lcp(s[pre], s[cur]);
FOR(j, 1, sz(s[pre]) - len) ans.pb('-');
FOR(j, len, sz(s[cur]) - 1) ans.pb(s[cur][j]);
ans.pb('P');
}
FOR(i, 0, sz(ans) - 1) cout << ans[i] << (i == sz(ans) - 1? "" : "\n");
return 0;
}
/*
3
print
the
poem
*/
/*
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Compilation message
printer.cpp: In function 'int dp(int, bool)':
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:73:3: note: in expansion of macro 'FOR'
73 | FOR(z, 0, 25) {
| ^~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:83:2: note: in expansion of macro 'FOR'
83 | FOR(z, 0, 25) {
| ^~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:90:2: note: in expansion of macro 'FOR'
90 | FOR(z, 0, 25) {
| ^~~
printer.cpp: In function 'void trace(int, bool)':
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:105:3: note: in expansion of macro 'FOR'
105 | FOR(z, 0, 25) {
| ^~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:113:2: note: in expansion of macro 'FOR'
113 | FOR(z, 0, 25) {
| ^~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:121:2: note: in expansion of macro 'FOR'
121 | FOR(z, 0, 25) {
| ^~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:128:2: note: in expansion of macro 'FOR'
128 | FOR(z, 0, 25) {
| ^~~
printer.cpp: In function 'int lcp(str&, str&)':
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:137:2: note: in expansion of macro 'FOR'
137 | FOR(i, 0, min(sz(a), sz(b)) - 1) {
| ^~~
printer.cpp: In function 'int main()':
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:148:5: note: in expansion of macro 'FOR'
148 | FOR(i, 1, n) cin >> s[i];
| ^~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:149:5: note: in expansion of macro 'FOR'
149 | FOR(i, 1, n) add(root, 0, s[i], i);
| ^~~
printer.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define EACH(i, x) for (auto &(i) : (x))
| ^
printer.cpp:153:5: note: in expansion of macro 'EACH'
153 | EACH(i, s[p[0]]) ans.pb(i);
| ^~~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:155:5: note: in expansion of macro 'FOR'
155 | FOR(i, 1, n - 1) {
| ^~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:157:6: note: in expansion of macro 'FOR'
157 | FOR(j, 1, sz(s[pre]) - len) ans.pb('-');
| ^~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:158:6: note: in expansion of macro 'FOR'
158 | FOR(j, len, sz(s[cur]) - 1) ans.pb(s[cur][j]);
| ^~~
printer.cpp:30:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
30 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
printer.cpp:161:5: note: in expansion of macro 'FOR'
161 | FOR(i, 0, sz(ans) - 1) cout << ans[i] << (i == sz(ans) - 1? "" : "\n");
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
61728 KB |
Line "" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
61768 KB |
Output is correct |
2 |
Incorrect |
30 ms |
61740 KB |
Expected EOF |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
61700 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
61852 KB |
Output is correct |
2 |
Runtime error |
78 ms |
124964 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
61720 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
61780 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
62008 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
62436 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
63188 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
61 ms |
62808 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |