답안 #52924

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52924 2018-06-27T07:45:35 Z 윤교준(#1384) 괄호 문자열 (CEOI16_match) C++11
0 / 100
3 ms 376 KB
#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define upmin(a,b) (a)=min((a),(b))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 2005;

int C[MAXN];
bitset<MAXN> B[MAXN];

char A[MAXN], Ans[MAXN];

int N;

void f(int s, int e) {
	if(s+1 == e) {
		Ans[s] = '(';
		Ans[e] = ')';
		return;
	}

	if(B[s+1][e-1]) {
		Ans[s] = '(';
		Ans[e] = ')';
		f(s+1, e-1);
		return;
	}

	for(int i = e-2; s < i; i -= 2) {
		if(A[s] != A[i] || !B[i+1][e]) continue;
		if(s+1 < i && !B[s+1][i-1]) continue;
		Ans[s] = '(';
		Ans[i] = ')';
		if(s+1 < i) f(s+1, i-1);
		f(i+1, e);
	}
}

int main() {
	scanf(" %s", A+1);
	N = int(strlen(A+1));
	if(N&1) {
		puts("-1");
		return 0;
	}

	for(int i = 1; i < N; i++)
		B[i][i+1] = (A[i] == A[i+1]);
	for(int d = 3; d < N; d += 2) {
		for(int i = 1; i+d <= N; i++) {
			if(A[i] == A[i+d] && B[i+1][i+d-1]) {
				B[i][i+d] = true;
				continue;
			}
			for(int k = i+1; k < i+d; k += 2) {
				if(B[i][k] && B[k+1][i+d]) {
					B[i][i+d] = true;
					break;
				}
			}
		}
	}

	for(int key = 0; key < (1<<N); key++) {
		for(int i = 0; i < N; i++) {
			Ans[i+1] = (key & (1<<i)) ? ')' : '(';
		}

		vector<int> V;
		bool flag = false;
		for(int i = 1; i <= N; i++) {
			if('(' == Ans[i]) {
				V.pb(i);
			} else {
				if(V.empty()) {
					flag = true;
					break;
				}
				C[V.back()] = i;
				C[i] = V.back();
				V.pop_back();

				if(A[i] != A[C[i]]) {
					flag = true;
					break;
				}
			}
		}
		if(sz(V) || flag) continue;

		for(int i = 1; i <= N; i++)
			putchar(Ans[i]);
		puts("");
		return 0;
	}

	return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf(" %s", A+1);
  ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -