| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 643284 | ghostwriter | Type Printer (IOI08_printer) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
debug(p);
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
*/
