제출 #75099

#제출 시각아이디문제언어결과실행 시간메모리
75099Yehezkiel괄호 문자열 (CEOI16_match)C++11
37 / 100
2044 ms944 KiB
#include <bits/stdc++.h>
using namespace std;
const int MAXN=2000;
int n;
string input;
bool coba(int idx,stack<char> characters){
    for(int i = idx; i < n ;  i++) {
        if(!characters.empty() && characters.top() == input[i]) {
            characters.pop();
        } else {
            characters.push(input[i]);
        }            
    }
    return characters.empty();
}

bool possible(){
    stack <char> temp;
    return coba(0,temp);
}

string makeAns(){
    string ret="";

    stack <char> characters;
    for(int i = 0; i < n; i++) {
        characters.push(input[i]);

        if(characters.empty()||characters.top() != input[i]) {
            ret += "(";
        } else if(coba(i+1,characters)) {
            ret += "(";
        } else {
            characters.pop();
            characters.pop();
            ret += ")";
        }
    }

    return ret;
}

int main()
{
    cin>>input;
    n = input.length();

    if (possible()) {
        cout<<makeAns()<<endl;
    } else {
        printf("-1\n");
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...