답안 #681393

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
681393 2023-01-13T00:22:49 Z vjudge1 괄호 문자열 (CEOI16_match) C++14
37 / 100
978 ms 32360 KB
#include <bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef pair<int, int> ii;

const int N = 2000 + 5;
const int mod = 1e9 + 7;
int f[N][N];
string s;
string t;
int n;

int dp(int l, int r)
{
    if(l > r) return 1;
    if((r - l + 1) & 1) return 0;
    int &res = f[l][r];
    if(~res) return res;
    
    res = 0;
    if(s[l] == s[r]) res |= dp(l + 1, r - 1);
    for(int i = r - 1; i > l; i--)
        if(s[l] == s[i]) res |= (dp(l + 1, i - 1) && dp(i + 1, r));
    return res;
}

void trace(int l, int r)
{
    if(l > r) return;
    
    if(s[l] == s[r])
    {
        t[l] = '(', t[r] = ')';
        trace(l + 1, r - 1);
        return;
    }
    for(int i = r - 1; i > l; i--) if((dp(l + 1, i - 1) && dp(i + 1, r)))
    {
        t[l] = '(', t[i] = ')';
        trace(l + 1, i - 1);
        trace(i + 1, r);
        return;
    }
}

void solve()
{
    cin >> s;
    int n = s.size();
    t.resize(n);
    memset(f, -1, sizeof f);
    if(dp(0, n - 1))
    {
        trace(0, n - 1);
        cout << t;
    }
    else cout << -1;
}

signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15956 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
3 Correct 6 ms 15956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15956 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
3 Correct 6 ms 15956 KB Output is correct
4 Correct 63 ms 16040 KB Output is correct
5 Correct 13 ms 16068 KB Output is correct
6 Correct 978 ms 16112 KB Output is correct
7 Correct 718 ms 16084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15956 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
3 Correct 6 ms 15956 KB Output is correct
4 Correct 63 ms 16040 KB Output is correct
5 Correct 13 ms 16068 KB Output is correct
6 Correct 978 ms 16112 KB Output is correct
7 Correct 718 ms 16084 KB Output is correct
8 Runtime error 357 ms 32360 KB Execution killed with signal 11
9 Halted 0 ms 0 KB -