답안 #826685

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

	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);
		if (it == begin(oc[c])) {
			cout << -1 << nl;
			exit(0-0);
		}

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

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

		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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 0 ms 212 KB Output isn't correct
4 Halted 0 ms 0 KB -