답안 #923161

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
923161 2024-02-06T18:57:41 Z JakobZorz 괄호 문자열 (CEOI16_match) C++17
10 / 100
1 ms 604 KB
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
#pragma GCC optimize ("03,unroll-loops")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
 
int n;
string str;
string res;
bool is_possible(int i,vector<char>p){
    for(int j=i;j<n;j++){
        if(p.empty()||p.back()!=str[j]){
            p.push_back(str[j]);
        }else{
            p.pop_back();
        }
    }
    
    return p.empty();
}

void solve(int l,int r){
    if(l==r)
        return;
    for(int i=r-1;;i--){
        if(str[i]==str[l]){
            res[i]=')';
            res[l]='(';
            solve(l+1,i);
            solve(i+1,r);
            return;
        }
    }
}
 
void solve(){
    cin>>str;
    n=(int)str.size();
    if(!is_possible(0,{})){
        cout<<"-1\n";
        return;
    }
    res=string(n,' ');
    solve(0,n);
    cout<<res<<"\n";
}
 
int main(){
    ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
    //freopen("input.in","r",stdin);freopen("output.out","w",stdout);
    int t=1;//cin>>t;
    while(t--)solve();
    return 0;
}
 
/*
 
abbaaa
(()())
 
abab
-1
 
 */
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Runtime error 1 ms 604 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Runtime error 1 ms 604 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -