#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
328 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
328 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
39 ms |
336 KB |
Output is correct |
10 |
Correct |
24 ms |
340 KB |
Output is correct |
11 |
Correct |
43 ms |
340 KB |
Output is correct |
12 |
Correct |
1576 ms |
556 KB |
Output is correct |
13 |
Correct |
1790 ms |
556 KB |
Output is correct |
14 |
Execution timed out |
2071 ms |
596 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |