#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
typedef pair<int,int> pii;
int n;
string s,sol;
stack<char> st;
bool can[2005][2005];
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++)
{
while(!st.empty())
st.pop();
for(int j=i;j<=n;j++)
{
if(!st.empty()&&st.top()==s[j])
st.pop();
else
st.push(s[j]);
if(st.empty())
can[i][j]=1;
}
}
if(!can[1][n])
{
cout<<-1;
return 0;
}
sol.resize(n+1);
for(int i=1;i<=n;i++)
if(sol[i]!=')')
{
sol[i]='(';
int poz=0;
for(int j=i+1;j<=n;j+=2)
{
if(sol[j]==')')
break;
if(can[i][j]&&s[i]==s[j])
if(j==i+1||can[i+1][j-1])
poz=j;
}
sol[poz]=')';
}
for(int i=1;i<=n;i++)
cout<<sol[i];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
1748 KB |
Output is correct |
5 |
Correct |
4 ms |
1740 KB |
Output is correct |
6 |
Correct |
6 ms |
3156 KB |
Output is correct |
7 |
Correct |
9 ms |
3832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
1748 KB |
Output is correct |
5 |
Correct |
4 ms |
1740 KB |
Output is correct |
6 |
Correct |
6 ms |
3156 KB |
Output is correct |
7 |
Correct |
9 ms |
3832 KB |
Output is correct |
8 |
Runtime error |
73 ms |
7520 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |