Submission #826687

# Submission time Handle Problem Language Result Execution time Memory
826687 2023-08-15T20:19:40 Z NK_ Match (CEOI16_match) C++17
100 / 100
17 ms 20300 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 1 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 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 0 ms 468 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 1 ms 212 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 0 ms 468 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 12240 KB Output is correct
13 Correct 9 ms 13272 KB Output is correct
14 Correct 10 ms 14708 KB Output is correct
15 Correct 12 ms 16196 KB Output is correct
16 Correct 11 ms 16196 KB Output is correct
17 Correct 12 ms 17356 KB Output is correct
18 Correct 12 ms 16716 KB Output is correct
19 Correct 14 ms 18968 KB Output is correct
20 Correct 8 ms 12608 KB Output is correct
21 Correct 17 ms 20300 KB Output is correct