#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2001;
const int AL = 26;
int n, dp[N][N], match[N];
string s;
void readInput(){
cin >> s;
n = s.size();
s = '*' + s;
}
void calcDp(int l){
stack <int> st;
dp[l][l - 1] = 1;
for(int i = l; i <= n; i++){
if (st.empty())
st.push(i);
else if (s[st.top()] == s[i])
st.pop();
else
st.push(i);
if (st.empty())
dp[l][i] = 1;
}
}
void process(int l, int r){
if (l > r)
return;
for(int i = r; i >= l; i--)
if (s[l] == s[i] && dp[l + 1][i - 1]){
match[l] = i;
match[i] = l;
process(l + 1, i - 1);
process(i + 1, r);
return;
}
}
void solve(){
for(int i = 1; i <= n; i++)
calcDp(i);
if (!dp[1][n]){
cout << -1;
return;
}
process(1, n);
for(int i = 1; i <= n; i++)
cout << (match[i] > i? '(' : ')');
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
readInput();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
6 ms |
4460 KB |
Output is correct |
5 |
Correct |
6 ms |
4588 KB |
Output is correct |
6 |
Correct |
13 ms |
9600 KB |
Output is correct |
7 |
Correct |
20 ms |
10348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
6 ms |
4460 KB |
Output is correct |
5 |
Correct |
6 ms |
4588 KB |
Output is correct |
6 |
Correct |
13 ms |
9600 KB |
Output is correct |
7 |
Correct |
20 ms |
10348 KB |
Output is correct |
8 |
Incorrect |
94 ms |
9964 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |