답안 #339697

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
339697 2020-12-26T00:02:55 Z couplefire 괄호 문자열 (CEOI16_match) C++17
100 / 100
20 ms 12160 KB
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005

int n; string s;
string ans;
int dp[MAXN][26];

bool solve(int l, int r){
    if(l == r+1) return 1;
    if(dp[r][s[l]-'a'] < l) return false;
    ans[l] = '(', ans[dp[r][s[l]-'a']-1] = ')';
    return solve(l+1, dp[r][s[l]-'a']-2) && solve(dp[r][s[l]-'a'], r);
}

int main(){
    // freopen("match.in", "r", stdin);
    // freopen("match.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> s; n = s.length(); ans.resize(n);
    for(int i = 0; i<n; i++){
        for(int j = 0; j<26; j++){
            dp[i][j] = -1;
            if(s[i]-'a' == j) dp[i][j] = i+1;
            if(i >= 1 && dp[i-1][s[i]-'a']-2 >= 0) dp[i][j] = max(dp[i][j], dp[dp[i-1][s[i]-'a']-2][j]);
        }
    }
    if(!solve(0, n-1)) cout << -1 << endl;
    else cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 2 ms 1004 KB Output is correct
9 Correct 2 ms 1132 KB Output is correct
10 Correct 2 ms 1132 KB Output is correct
11 Correct 2 ms 1260 KB Output is correct
12 Correct 10 ms 7404 KB Output is correct
13 Correct 11 ms 8044 KB Output is correct
14 Correct 13 ms 8812 KB Output is correct
15 Correct 16 ms 10348 KB Output is correct
16 Correct 16 ms 10348 KB Output is correct
17 Correct 16 ms 10880 KB Output is correct
18 Correct 18 ms 10476 KB Output is correct
19 Correct 17 ms 11372 KB Output is correct
20 Correct 11 ms 7660 KB Output is correct
21 Correct 20 ms 12160 KB Output is correct