제출 #341621

#제출 시각아이디문제언어결과실행 시간메모리
341621phathnv괄호 문자열 (CEOI16_match)C++11
37 / 100
94 ms10348 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2001; const int AL = 26; int n, dp[N][N], match[N]; string s; void readInput(){ cin >> s; n = s.size(); s = '*' + s; } void calcDp(int l){ stack <int> st; dp[l][l - 1] = 1; for(int i = l; i <= n; i++){ if (st.empty()) st.push(i); else if (s[st.top()] == s[i]) st.pop(); else st.push(i); if (st.empty()) dp[l][i] = 1; } } void process(int l, int r){ if (l > r) return; for(int i = r; i >= l; i--) if (s[l] == s[i] && dp[l + 1][i - 1]){ match[l] = i; match[i] = l; process(l + 1, i - 1); process(i + 1, r); return; } } void solve(){ for(int i = 1; i <= n; i++) calcDp(i); if (!dp[1][n]){ cout << -1; return; } process(1, n); for(int i = 1; i <= n; i++) cout << (match[i] > i? '(' : ')'); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); readInput(); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...