Submission #826686

# Submission time Handle Problem Language Result Execution time Memory
826686 2023-08-15T20:19:22 Z NK_ Match (CEOI16_match) C++17
100 / 100
15 ms 20428 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using vi = V<int>;
using ll = long long;
using pl = pair<ll, ll>;
using vl = V<ll>;

const int nax = 1e5;
 
int main() {
	cin.tie(0)->sync_with_stdio(0);
 
	string S; cin >> S;
	int N = sz(S);

	string ans(N, '-');

	V<vi> oc(N + 1);
	vi P(N + 1); // prv
	V<vi> nxt(N + 1, vi(26, -1));
	int id = 1;

 	auto go = [&](int i, int c) {
 		if (nxt[i][c] == -1) {
 			nxt[i][c] = id++;
 			P[nxt[i][c]] = i;
 		}
 		return nxt[i][c];
 	};

 	int cur = 0;
 	vi C = {0}; oc[0].pb(-1);
	for(int i = 0; i < N; i++) {
		// cout << i << " " << cur << endl;
		if (cur && S[oc[P[cur]].back() + 1] == S[i]) {
			cur = P[cur]; // go back (take off stack)
		} else {
			cur = go(cur, S[i] - 'a'); // go to next (put on stack)
		}
		// cout << i << " " << cur << endl;
		oc[cur].pb(i); C.pb(cur);
	}

	if (cur != 0) {
		cout << -1 << nl;
		exit(0-0);
	}

	function<void(int, int)> dnc = [&](int l, int r) {
		if (l >= r) return;

		int c = C[l + 1];
		auto it = upper_bound(begin(oc[c]), end(oc[c]), r);

		int m = *prev(it); ++m;

		ans[l] = '(', ans[m] = ')';
		dnc(l + 1, m - 1); dnc(m + 1, r);
	}; 

	dnc(0, N - 1);

	cout << ans << nl;

	exit(0-0);
}				
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 1 ms 532 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 1 ms 532 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1620 KB Output is correct
10 Correct 1 ms 1620 KB Output is correct
11 Correct 1 ms 1748 KB Output is correct
12 Correct 8 ms 12288 KB Output is correct
13 Correct 9 ms 13320 KB Output is correct
14 Correct 13 ms 14796 KB Output is correct
15 Correct 14 ms 16204 KB Output is correct
16 Correct 12 ms 16204 KB Output is correct
17 Correct 13 ms 17420 KB Output is correct
18 Correct 13 ms 16716 KB Output is correct
19 Correct 14 ms 19140 KB Output is correct
20 Correct 9 ms 12676 KB Output is correct
21 Correct 15 ms 20428 KB Output is correct