#include<bits/stdc++.h>
using namespace std;
const int nmax=1e5+42;
int n;
string inp;
string outp;
stack<int> active,mem;
bool check(int pos)
{
while(pos<n)
{
if(active.size()==0)active.push(pos);
else if(inp[active.top()]==inp[pos])active.pop();
else active.push(pos);
pos++;
}
return active.size()==0;
}
int main()
{
cin>>inp;
n=inp.size();
if(check(0)==0)
{
printf("-1\n");
return 0;
}
for(int i=0;i<n;i++)
{
mem=active;
active.push(i);
bool possible=check(i+1);
active=mem;
if(possible)
{
outp.push_back('(');
active.push(i);
}
else
{
outp.push_back(')');
active.pop();
}
}
cout<<outp<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
7 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
384 KB |
Output is correct |
7 |
Correct |
14 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
7 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
384 KB |
Output is correct |
7 |
Correct |
14 ms |
384 KB |
Output is correct |
8 |
Correct |
123 ms |
384 KB |
Output is correct |
9 |
Correct |
148 ms |
504 KB |
Output is correct |
10 |
Correct |
130 ms |
384 KB |
Output is correct |
11 |
Correct |
117 ms |
384 KB |
Output is correct |
12 |
Execution timed out |
2089 ms |
772 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |