답안 #826601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
826601 2023-08-15T17:38:49 Z NK_ 괄호 문자열 (CEOI16_match) C++17
10 / 100
1 ms 340 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;

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

		int m = *prev(it);

		if (l > m) {
			cout << -1 << nl;
			exit(0-0);
		}

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

	dnc(0, N - 1);

	cout << ans << nl;

	exit(0-0);
}				
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 0 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 0 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -