#include <bits/stdc++.h>
using namespace std;
int n;
string s;
bool check(stack<char>st,int l)
{
for(int i=l;i<n;i++)
{
if(st.empty()||st.top()!=s[i])
{
st.push(s[i]);
}
else if(!st.empty()&&st.top()==s[i])
{
st.pop();
}
}
if(!st.empty())
{
return false;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>s;
n=s.size();
stack<char>st;
string res="";
for(int i=0;i<n;i++)
{
if(st.empty()||st.top()!=s[i])
{
res+='(';
st.push(s[i]);
}
else if(!st.empty()&&st.top()==s[i])
{
st.push(s[i]);
if(check(st,i+1))
{
res+='(';
}
else
{
st.pop();
st.pop();
res+=')';
}
}
}
if(!st.empty())
{
cout<<"-1";
return 0;
}
cout<<res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
23 ms |
344 KB |
Output is correct |
9 |
Correct |
29 ms |
476 KB |
Output is correct |
10 |
Correct |
27 ms |
480 KB |
Output is correct |
11 |
Correct |
23 ms |
460 KB |
Output is correct |
12 |
Correct |
1753 ms |
600 KB |
Output is correct |
13 |
Correct |
1863 ms |
856 KB |
Output is correct |
14 |
Execution timed out |
2056 ms |
952 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |