답안 #608763

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
608763 2022-07-27T09:57:11 Z Vanilla 괄호 문자열 (CEOI16_match) C++17
10 / 100
270 ms 16040 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long int64;
const int maxn = 2e3 + 2;
int dp[maxn][maxn];
string rs;
string s;
int n;

bool solve (int l, int r) {
    if (l > r) return 1;
    if ((r - l + 1) % 2 == 1) return 0;
    if (dp[l][r] != -1) return dp[l][r];
    dp[l][r] = 0;
    if (s[l] == s[r]) {
        rs[l] = '(', rs[r] = ')';
        return dp[l][r] = solve(l + 1, r - 1);
    }
    for (int i = l + 1; i < r; i++){
        dp[l][r] |= (solve(l, i) & solve(i + 1, r));
    }
    return dp[l][r];
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> s;
    n = s.size();
    rs = string(n, '0');
    memset(dp, -1, sizeof(dp));
    cout << (solve(0, n - 1) == 1 ? rs: "-1");
    return 0;
}

Compilation message

match.cpp: In function 'bool solve(int, int)':
match.cpp:19:25: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   19 |         return dp[l][r] = solve(l + 1, r - 1);
      |                ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15932 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
3 Correct 9 ms 16000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15932 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
3 Correct 9 ms 16000 KB Output is correct
4 Incorrect 270 ms 16040 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15932 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
3 Correct 9 ms 16000 KB Output is correct
4 Incorrect 270 ms 16040 KB Output isn't correct
5 Halted 0 ms 0 KB -