Submission #643341

# Submission time Handle Problem Language Result Execution time Memory
643341 2022-09-21T18:34:35 Z ghostwriter Type Printer (IOI08_printer) C++14
100 / 100
204 ms 76532 KB
#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 next[26], cnt;
	vi leaf;
	Node() { memset(next, 0, sizeof next); cnt = 0; }
};
const int oo = 1e9 + 5;
const int M = 5e5 + 5;
const int N = 25e3 + 5;
int n, d[M][2], 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[cur].cnt;
	if (h >= sz(s)) {
		trie[cur].leaf.pb(pos);
		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 != oo) return rs;
	if (trie[i].cnt == 1 && !trie[i].leaf.empty()) {
		int len = sz(s[trie[i].leaf[0]]);
		if (!j) rs = 2 * len + 1;
		else rs = len + 1;
		return rs;
	}
	int nv = 0;
	EACH(z, trie[i].leaf) nv += 2 * sz(s[z]) + 1;
	if (!j) {
		rs = 0;
		FOR(z, 0, 25) {
			int np = trie[i].next[z];
			if (!np) continue;
			int cnt = trie[np].cnt;
			rs += -2 * (cnt - 1) + dp(np, 0);
		}
		rs += nv;
		return rs;
	}
	int cnt0 = 0, tcnt = 0;
	FOR(z, 0, 25) {
		int np = trie[i].next[z];
		if (!np) continue;
		tcnt -= 2 * (trie[np].cnt - 1);
		cnt0 += dp(np, 0);
	}
	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);
	}
	rs += nv;
	return rs;
}
void trace(int i, bool j) {
	if (trie[i].cnt == 1 && !trie[i].leaf.empty()) {
		p.pb(trie[i].leaf[0]);
		return;
	}
	int nv = 0;
	EACH(z, trie[i].leaf) {
		nv += 2 * sz(s[z]) + 1;
		p.pb(z);
	}
	if (!j) {
		FOR(z, 0, 25) {
			int np = trie[i].next[z];
			if (!np) continue;
			trace(np, 0);
		}
		return;
	}
	int cnt0 = 0, tcnt = 0;
	FOR(z, 0, 25) {
		int np = trie[i].next[z];
		if (!np) continue;
		tcnt -= 2 * (trie[np].cnt - 1);
		cnt0 += dp(np, 0);
	}
	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 + nv == cur) {
			pos = z;
			break;
		}
	}
	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);
    FOR(i, 1, cnt)
    FOR(j, 0, 1)
    	d[i][j] = oo;
    cout << dp(1, 1) << '\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');
    }
    EACH(i, ans) cout << i << '\n';
    return 0;
}
/*
3
aaaa
bb
bbc
*/
/*
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:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
printer.cpp:72:2: note: in expansion of macro 'EACH'
   72 |  EACH(z, trie[i].leaf) nv += 2 * sz(s[z]) + 1;
      |  ^~~~
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:75:3: note: in expansion of macro 'FOR'
   75 |   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:85:2: note: in expansion of macro 'FOR'
   85 |  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:91:2: note: in expansion of macro 'FOR'
   91 |  FOR(z, 0, 25) {
      |  ^~~
printer.cpp: In function 'void trace(int, bool)':
printer.cpp:32:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   32 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
printer.cpp:107:2: note: in expansion of macro 'EACH'
  107 |  EACH(z, trie[i].leaf) {
      |  ^~~~
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:112:3: note: in expansion of macro 'FOR'
  112 |   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:120:2: note: in expansion of macro 'FOR'
  120 |  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:127:2: note: in expansion of macro 'FOR'
  127 |  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:137:2: note: in expansion of macro 'FOR'
  137 |  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:146:2: note: in expansion of macro 'FOR'
  146 |  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:157:5: note: in expansion of macro 'FOR'
  157 |     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:158:5: note: in expansion of macro 'FOR'
  158 |     FOR(i, 1, n) add(root, 0, s[i], 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:159:5: note: in expansion of macro 'FOR'
  159 |     FOR(i, 1, cnt)
      |     ^~~
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:160:5: note: in expansion of macro 'FOR'
  160 |     FOR(j, 0, 1)
      |     ^~~
printer.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
printer.cpp:164:5: note: in expansion of macro 'EACH'
  164 |     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:166:5: note: in expansion of macro 'FOR'
  166 |     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:168:6: note: in expansion of macro 'FOR'
  168 |      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:169:6: note: in expansion of macro 'FOR'
  169 |      FOR(j, len, sz(s[cur]) - 1) ans.pb(s[cur][j]);
      |      ^~~
printer.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
printer.cpp:172:5: note: in expansion of macro 'EACH'
  172 |     EACH(i, ans) cout << i << '\n';
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 67540 KB Output is correct
2 Correct 42 ms 67524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 67644 KB Output is correct
2 Correct 32 ms 67576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 67556 KB Output is correct
2 Correct 33 ms 67536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 67628 KB Output is correct
2 Correct 33 ms 67580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 67532 KB Output is correct
2 Correct 33 ms 67740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 67756 KB Output is correct
2 Correct 34 ms 67788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 68112 KB Output is correct
2 Correct 50 ms 68800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 69184 KB Output is correct
2 Correct 41 ms 68564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 71348 KB Output is correct
2 Correct 166 ms 74984 KB Output is correct
3 Correct 110 ms 72792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 70748 KB Output is correct
2 Correct 204 ms 76532 KB Output is correct
3 Correct 109 ms 73412 KB Output is correct
4 Correct 147 ms 76140 KB Output is correct