Submission #36889

# Submission time Handle Problem Language Result Execution time Memory
36889 2017-12-17T13:21:58 Z aome Match (CEOI16_match) C++14
100 / 100
33 ms 18112 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() {
	// freopen("in.txt", "r", stdin);
	// freopen("out.txt", "w", stdout);
	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];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 17740 KB Output is correct
2 Correct 0 ms 17740 KB Output is correct
3 Correct 0 ms 17740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 17740 KB Output is correct
2 Correct 0 ms 17740 KB Output is correct
3 Correct 0 ms 17740 KB Output is correct
4 Correct 0 ms 17740 KB Output is correct
5 Correct 0 ms 17740 KB Output is correct
6 Correct 0 ms 17740 KB Output is correct
7 Correct 0 ms 17740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 17740 KB Output is correct
2 Correct 0 ms 17740 KB Output is correct
3 Correct 0 ms 17740 KB Output is correct
4 Correct 0 ms 17740 KB Output is correct
5 Correct 0 ms 17740 KB Output is correct
6 Correct 0 ms 17740 KB Output is correct
7 Correct 0 ms 17740 KB Output is correct
8 Correct 3 ms 17740 KB Output is correct
9 Correct 0 ms 17740 KB Output is correct
10 Correct 3 ms 17740 KB Output is correct
11 Correct 0 ms 17740 KB Output is correct
12 Correct 16 ms 17876 KB Output is correct
13 Correct 9 ms 18056 KB Output is correct
14 Correct 26 ms 18056 KB Output is correct
15 Correct 19 ms 18056 KB Output is correct
16 Correct 23 ms 18056 KB Output is correct
17 Correct 33 ms 18056 KB Output is correct
18 Correct 33 ms 18056 KB Output is correct
19 Correct 19 ms 18056 KB Output is correct
20 Correct 16 ms 17876 KB Output is correct
21 Correct 33 ms 18112 KB Output is correct