#include <bits/stdc++.h>
using namespace std;
string str;
int dp[2001][2001];
bool go(int s, int e){
if(s>e){
return true;
}
if(dp[s][e]!=-1){
return dp[s][e]==1;
}
bool ret = false;
for(int i = s+1; i<=e && !ret; i++){
ret |= (str[s]==str[i]) && go(s+1,i-1)&&go(i+1,e);
}
if(ret){
dp[s][e] = 1;
}
else{
dp[s][e] = 0;
}
return ret;
}
string ans = "";
void build(int s, int e){
if(s>e){
return;
}
for(int i = e; i>=s+1; i--){
if(str[s]==str[i] && go(s+1,i-1) && go(i+1,e)){
ans += "(";
build(s+1,i-1);
ans += ")";
build(i+1,e);
return;
}
}
ans += "ERROR";
}
int main(){
cin >> str;
assert(str.length()<=2000);
for(int a = 0; a<2001; a++){
for(int b = 0; b<2001; b++){
dp[a][b] = -1;
}
}
if(go(0,str.length()-1)){
build(0,str.length()-1);
cout << ans<< endl;
}
else{
cout << -1 << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15992 KB |
Output is correct |
2 |
Correct |
14 ms |
16000 KB |
Output is correct |
3 |
Correct |
13 ms |
16032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15992 KB |
Output is correct |
2 |
Correct |
14 ms |
16000 KB |
Output is correct |
3 |
Correct |
13 ms |
16032 KB |
Output is correct |
4 |
Correct |
116 ms |
16164 KB |
Output is correct |
5 |
Correct |
20 ms |
16228 KB |
Output is correct |
6 |
Correct |
848 ms |
16312 KB |
Output is correct |
7 |
Correct |
1780 ms |
16516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15992 KB |
Output is correct |
2 |
Correct |
14 ms |
16000 KB |
Output is correct |
3 |
Correct |
13 ms |
16032 KB |
Output is correct |
4 |
Correct |
116 ms |
16164 KB |
Output is correct |
5 |
Correct |
20 ms |
16228 KB |
Output is correct |
6 |
Correct |
848 ms |
16312 KB |
Output is correct |
7 |
Correct |
1780 ms |
16516 KB |
Output is correct |
8 |
Runtime error |
5 ms |
16516 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Halted |
0 ms |
0 KB |
- |