답안 #103668

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
103668 2019-04-01T23:29:53 Z Anai 괄호 문자열 (CEOI16_match) C++14
100 / 100
18 ms 11776 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5, S = 26;


char str[N], ant[N];
int dp[S][N];

int n;

static void solve(int l, int r) {
	if (l > r)
		return;
	int match = dp[str[l] - 'a'][r];

	if (!match)
		throw -1;

	ant[l] = '(';
	ant[match] = ')';

	solve(l + 1, match - 1);
	solve(match + 1, r); }

int main() {
#ifdef HOME
	freopen("match.in", "r", stdin);
	freopen("match.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	vector<int> stk;
	stk.reserve(N);

	cin >> (str + 1);
	n = strlen(str + 1);

	for (int i = 1; i <= n; ++i) {
		dp[str[i] - 'a'][i] = i;
		if (i == 1)
			continue;

		int pos = dp[str[i] - 'a'][i - 1];
		if (pos <= 1)
			continue;

		for (int ch = 0; ch < S; ++ch) {
			if (str[i] != ch + 'a')
				dp[ch][i] = dp[ch][pos - 1]; } }

	try {
		solve(1, n); }
	catch (int e) {
		cout << "-1\n";
		return 0; }

	cout << (ant + 1) << endl;

	return 0; }
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 4 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 3 ms 1152 KB Output is correct
9 Correct 4 ms 1280 KB Output is correct
10 Correct 3 ms 1280 KB Output is correct
11 Correct 3 ms 1280 KB Output is correct
12 Correct 10 ms 7296 KB Output is correct
13 Correct 12 ms 7852 KB Output is correct
14 Correct 12 ms 8576 KB Output is correct
15 Correct 12 ms 9984 KB Output is correct
16 Correct 13 ms 9984 KB Output is correct
17 Correct 16 ms 10496 KB Output is correct
18 Correct 13 ms 10368 KB Output is correct
19 Correct 18 ms 11136 KB Output is correct
20 Correct 14 ms 7472 KB Output is correct
21 Correct 17 ms 11776 KB Output is correct