Submission #643323

# Submission time Handle Problem Language Result Execution time Memory
643323 2022-09-21T18:06:19 Z ghostwriter Type Printer (IOI08_printer) C++14
0 / 100
39 ms 57292 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, pos;
	Node() { 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], 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;
	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');
    // }
    // EACH(i, ans) cout << 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:70:3: note: in expansion of macro 'FOR'
   70 |   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:79:2: note: in expansion of macro 'FOR'
   79 |  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:86:2: note: in expansion of macro 'FOR'
   86 |  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:101:3: note: in expansion of macro 'FOR'
  101 |   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:109:2: note: in expansion of macro 'FOR'
  109 |  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:117:2: note: in expansion of macro 'FOR'
  117 |  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:124:2: note: in expansion of macro 'FOR'
  124 |  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:133:2: note: in expansion of macro 'FOR'
  133 |  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:144:5: note: in expansion of macro 'FOR'
  144 |     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:145:5: note: in expansion of macro 'FOR'
  145 |     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:146:5: note: in expansion of macro 'FOR'
  146 |     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:147:5: note: in expansion of macro 'FOR'
  147 |     FOR(j, 0, 1)
      |     ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 55892 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 55820 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 55892 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 55892 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 55892 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 55912 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 56096 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 56328 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 33 ms 57292 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 56888 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -