#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 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 lg=2;lg<=n;lg+=2)
for(int i=1;i<=n;i++)
{
int j=i+lg-1;
if(j>n)
break;
if(j==i+1)
{
can[i][j]=(s[i]==s[j]);
continue;
}
if(s[i]==s[j])
can[i][j]|=can[i+1][j-1];
for(int k=i+1;k<j;k+=2)
can[i][j]|=(can[i][k]&can[k+1][j]);
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
31 ms |
2124 KB |
Output is correct |
5 |
Correct |
37 ms |
2252 KB |
Output is correct |
6 |
Correct |
153 ms |
3256 KB |
Output is correct |
7 |
Correct |
345 ms |
4244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
31 ms |
2124 KB |
Output is correct |
5 |
Correct |
37 ms |
2252 KB |
Output is correct |
6 |
Correct |
153 ms |
3256 KB |
Output is correct |
7 |
Correct |
345 ms |
4244 KB |
Output is correct |
8 |
Runtime error |
6 ms |
8436 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |