답안 #36888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
36888 2017-12-17T13:20:15 Z aome 괄호 문자열 (CEOI16_match) C++14
37 / 100
9 ms 18052 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100005;

int n;
char res[N];
string s;
pair<int, int> rmq[20][N];

void solve(int l, int r) {
	if (l > r) return;

	if (s[l] == s[r]) {
		res[l] = '(', res[r] = ')';
		solve(l + 1, r - 1); return;
	}

	int cur = r, type = s[l] - 'a';
	if (rmq[17][cur].second >> type & 1) {
		for (int j = 16; j >= 0; --j) {
			if (!(rmq[j][cur].second >> type & 1)) cur = rmq[j][cur].first;
		}
		cur = rmq[0][cur].first;
	}

	if (cur <= l || s[cur] != s[l]) {
		cout << -1; exit(0);
	}

	res[l] = '(', res[cur] = ')';
	solve(l + 1, cur - 1), solve(cur + 1, r);
}

int main() {
	ios::sync_with_stdio(false);
	cin >> s, n = s.size();

	for (int i = 0; i < n; ++i) {
		int type = s[i] - 'a';
		rmq[0][i].first = i;

		if (i) {
			if (s[i - 1] == s[i]) {
				if (i - 2 >= 0) rmq[0][i].first = i - 2, rmq[0][i].second = 1 << (s[i - 2] - 'a');
				else rmq[0][i].first = i;
			}

			else {
				int cur = i - 1;
				if (rmq[17][cur].second >> type & 1) {
					for (int j = 16; j >= 0; --j) {
						if (!(rmq[j][cur].second >> type & 1)) cur = rmq[j][cur].first;
					}

					cur = rmq[0][cur].first;
					if (cur) {
						cur--;
						rmq[0][i].first = cur;
						rmq[0][i].second = 1 << (s[cur] - 'a');
					}
				}
			}
		}

		// cout << i << ' ' << rmq[0][i].first << '\n';

		for (int j = 1; j <= 17; ++j) {
			int tmp = rmq[j - 1][i].first;
			rmq[j][i].first = rmq[j - 1][tmp].first;
			rmq[j][i].second |= rmq[j - 1][i].second | rmq[j - 1][tmp].second; 
		}

	}

	if (n & 1) {
		cout << -1; return 0;
	}

	solve(0, n - 1);
	for (int i = 0; i < n; ++i) cout << res[i];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 17900 KB Output is correct
2 Correct 0 ms 17900 KB Output is correct
3 Correct 0 ms 17900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 17900 KB Output is correct
2 Correct 0 ms 17900 KB Output is correct
3 Correct 0 ms 17900 KB Output is correct
4 Correct 0 ms 17900 KB Output is correct
5 Correct 0 ms 17900 KB Output is correct
6 Correct 0 ms 17900 KB Output is correct
7 Correct 0 ms 17900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 17900 KB Output is correct
2 Correct 0 ms 17900 KB Output is correct
3 Correct 0 ms 17900 KB Output is correct
4 Correct 0 ms 17900 KB Output is correct
5 Correct 0 ms 17900 KB Output is correct
6 Correct 0 ms 17900 KB Output is correct
7 Correct 0 ms 17900 KB Output is correct
8 Correct 0 ms 17900 KB Output is correct
9 Correct 0 ms 17900 KB Output is correct
10 Correct 3 ms 17900 KB Output is correct
11 Correct 0 ms 17900 KB Output is correct
12 Runtime error 9 ms 18052 KB Execution killed because of forbidden syscall writev (20)
13 Halted 0 ms 0 KB -