This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |