답안 #339681

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
339681 2020-12-25T22:59:40 Z couplefire 괄호 문자열 (CEOI16_match) C++17
0 / 100
2 ms 364 KB
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005

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

bool solve(int l, int r){
    if(l == r+1) return 1;
    if(dp[l] == -1) return false;
    ans[l] = '('; ans[dp[l]] = ')';
    return solve(l+1, dp[l]-1) && solve(dp[l]+1, 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);
    dp[n] = -1;
    for(int i = n-1; i>=0; i--){
        dp[i] = -1;
        if(i != n-1 && s[i] == s[i+1]) dp[i] = i+1;
        int cur = i+1;
        while(dp[cur] != -1){
            cur = dp[cur]+1;
            if(cur != n && s[cur] == s[i]) dp[i] = cur;
        }
    }
    if(!solve(0, n-1)) cout << -1 << endl;
    else cout << ans << endl;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   17 |     freopen("match.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   18 |     freopen("match.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 364 KB Output isn't correct
2 Halted 0 ms 0 KB -