제출 #588998

#제출 시각아이디문제언어결과실행 시간메모리
588998andrei_boaca괄호 문자열 (CEOI16_match)C++14
37 / 100
2071 ms596 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
typedef pair<int,int> pii;
int n;
string s,sol;
stack<char> st;
bitset<100005> can[100005];
int f[31];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>s;
    n=s.size();
    s=" "+s;
    if(n%2==1)
    {
        cout<<-1;
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        if(!st.empty()&&st.top()==s[i])
            st.pop();
        else
            st.push(s[i]);
    }
    if(!st.empty())
    {
        cout<<-1;
        return 0;
    }
    sol.resize(n+1);
    for(int i=1;i<=n;i++)
        if(sol[i]!=')')
        {
            sol[i]='(';
            while(!st.empty())
                st.pop();
            int poz=0;
            if(s[i+1]==s[i])
                poz=i+1;
            for(int j=i+1;j<=n;j++)
            {
                if(sol[j]==')')
                    break;
                if(!st.empty()&&st.top()==s[j])
                    st.pop();
                else
                    st.push(s[j]);
                if(st.empty()&&s[j+1]==s[i]&&sol[j+1]!=')')
                    poz=j+1;
            }
            sol[poz]=')';
        }
    for(int i=1;i<=n;i++)
        cout<<sol[i];
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...