이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |