Submission #826570

# Submission time Handle Problem Language Result Execution time Memory
826570 2023-08-15T17:08:55 Z NK_ Match (CEOI16_match) C++17
10 / 100
321 ms 524288 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>;
 
int main() {
	cin.tie(0)->sync_with_stdio(0);
 
	string S; cin >> S;
	int N = sz(S);

	string ans(N, '-');

	V<map<int, vector<int>>> oc(26); 

	vi P = {0};
	for(int i = 0; i < N; i++) {
		int c = S[i] - 'a'; P.pb(P.back() ^ (1 << c));
		// cout << i << " " << S[i] << " " << P.back() << endl;
		oc[c][P.back()].pb(i);
	}

	function<void(int, int)> dnc = [&](int l, int r) {
		if (l > r) return;
		int lc = S[l] - 'a';

		// cout << l << " " << r << endl;

		const vi &OC = oc[lc][P[l]];
		auto it = upper_bound(begin(OC), end(OC), r);
		if (it == begin(OC)) {
			cout << -1 << nl;
			exit(0-0);
		}

		int m = *prev(it);

		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 0 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 0 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 Runtime error 321 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 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 Runtime error 321 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -