# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
52950 | 2018-06-27T08:36:36 Z | 윤교준(#1384) | Match (CEOI16_match) | C++11 | 528 ms | 1760 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 > e) return; if(s+1 == e) { Ans[s] = '('; Ans[e] = ')'; return; } if(A[s] == A[e] && 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; Ans[s] = '('; Ans[i] = ')'; if(s+1 < i) f(s+1, i-1); f(i+1, e); return; } } 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; } } } } if(!B[1][N]) { puts("-1"); return 0; } f(1, N); for(int i = 1; i <= N; i++) putchar(Ans[i]); puts(""); return 0; /* for(int key = 0; key < (1<<N); key++) { for(int i = 0; i < N; i++) { Ans[N-i] = (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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 488 KB | Output is correct |
3 | Correct | 2 ms | 488 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 488 KB | Output is correct |
3 | Correct | 2 ms | 488 KB | Output is correct |
4 | Correct | 57 ms | 676 KB | Output is correct |
5 | Correct | 79 ms | 828 KB | Output is correct |
6 | Correct | 353 ms | 876 KB | Output is correct |
7 | Correct | 528 ms | 1100 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 488 KB | Output is correct |
3 | Correct | 2 ms | 488 KB | Output is correct |
4 | Correct | 57 ms | 676 KB | Output is correct |
5 | Correct | 79 ms | 828 KB | Output is correct |
6 | Correct | 353 ms | 876 KB | Output is correct |
7 | Correct | 528 ms | 1100 KB | Output is correct |
8 | Runtime error | 3 ms | 1760 KB | Execution killed with signal 11 (could be triggered by violating memory limits) |
9 | Halted | 0 ms | 0 KB | - |