Submission #643291

# Submission time Handle Problem Language Result Execution time Memory
643291 2022-09-21T17:08:45 Z ghostwriter Type Printer (IOI08_printer) C++14
20 / 100
147 ms 232104 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 h, next[26], cnt, pos;
	Node() { h = 0; memset(next, 0, sizeof next); cnt = pos = 0; }
};
const int oo = 1e9 + 5;
const int M = 1e6 + 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[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 != oo) return rs;
	if (trie[i].cnt == 1) {
		int len = sz(s[trie[i].pos]);
		if (!j) rs = 2 * len + 1;
		else rs = len + 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 += -2 * (cnt - 1) + dp(np, 0);
		}
		return rs;
	}
	int cnt0 = 0, cnt1 = 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);
		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 -= 2 * (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);
    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');
    }
    FOR(i, 0, sz(ans) - 1) cout << ans[i] << '\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:82:2: note: in expansion of macro 'FOR'
   82 |  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:89:2: note: in expansion of macro 'FOR'
   89 |  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:104:3: note: in expansion of macro 'FOR'
  104 |   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:112:2: 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: 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:136:2: note: in expansion of macro 'FOR'
  136 |  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:147:5: note: in expansion of macro 'FOR'
  147 |     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:148:5: note: in expansion of macro 'FOR'
  148 |     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:149:5: note: in expansion of macro 'FOR'
  149 |     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:150:5: note: in expansion of macro 'FOR'
  150 |     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:154:5: note: in expansion of macro 'EACH'
  154 |     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:156:5: note: in expansion of macro 'FOR'
  156 |     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:158:6: note: in expansion of macro 'FOR'
  158 |      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:159:6: note: in expansion of macro 'FOR'
  159 |      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:162:5: note: in expansion of macro 'FOR'
  162 |     FOR(i, 0, sz(ans) - 1) cout << ans[i] << '\n';
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 52 ms 114528 KB Output is correct
2 Correct 52 ms 114596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 114536 KB Output is correct
2 Correct 50 ms 114556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 53 ms 114608 KB Expected EOF
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 51 ms 114528 KB Output is correct
2 Runtime error 147 ms 232104 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 54 ms 114580 KB Expected EOF
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 52 ms 114720 KB Expected EOF
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 56 ms 115024 KB Expected EOF
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 72 ms 115736 KB Expected EOF
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 77 ms 117272 KB Expected EOF
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 78 ms 116740 KB Expected EOF
2 Halted 0 ms 0 KB -