#include <bits/stdc++.h>
#define N 100050
#define f first
#define s second
using namespace std;
int n, v[N];
string s;
char resp[N];
bool solve(int l, int r)
{
if(r < l) return 1;
if(r - l == 1)
{
resp[l] = '(', resp[r] = ')';
return (v[l] == v[r]);
}
if(l == r) return 0;
stack<int> open;
for(int j = r; j > l; j--)
{
if(!open.empty())
{
int t = open.top();
if(t == v[j]) open.pop();
else open.push(v[j]);
}
else
{
if(v[l] == v[j])
{
resp[l] = '(', resp[j] = ')';
bool A = solve(l + 1, j - 1);
if(A) return solve(j + 1, r);
else return 0;
return (solve(l + 1, j - 1) and solve(j + 1, r));
}
else open.push(v[j]);
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>s;
n = s.size();
for(int i = 1; i <= n; i++) v[i] = s[i - 1] - 'a';
if(solve(1, n))
{
for(int i = 1; i<= n; i++) cout<<resp[i];
cout<<"\n";
}
else cout<<"-1\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
412 KB |
Output is correct |
4 |
Correct |
4 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
616 KB |
Output is correct |
6 |
Correct |
3 ms |
1000 KB |
Output is correct |
7 |
Correct |
3 ms |
1000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
412 KB |
Output is correct |
4 |
Correct |
4 ms |
616 KB |
Output is correct |
5 |
Correct |
3 ms |
616 KB |
Output is correct |
6 |
Correct |
3 ms |
1000 KB |
Output is correct |
7 |
Correct |
3 ms |
1000 KB |
Output is correct |
8 |
Correct |
7 ms |
1000 KB |
Output is correct |
9 |
Correct |
18 ms |
2080 KB |
Output is correct |
10 |
Correct |
9 ms |
2080 KB |
Output is correct |
11 |
Correct |
9 ms |
2880 KB |
Output is correct |
12 |
Correct |
143 ms |
14016 KB |
Output is correct |
13 |
Correct |
95 ms |
15084 KB |
Output is correct |
14 |
Correct |
849 ms |
26204 KB |
Output is correct |
15 |
Correct |
40 ms |
26204 KB |
Output is correct |
16 |
Correct |
39 ms |
26204 KB |
Output is correct |
17 |
Correct |
141 ms |
26524 KB |
Output is correct |
18 |
Correct |
32 ms |
26524 KB |
Output is correct |
19 |
Correct |
1276 ms |
26524 KB |
Output is correct |
20 |
Correct |
527 ms |
26524 KB |
Output is correct |
21 |
Execution timed out |
2078 ms |
26524 KB |
Time limit exceeded |