-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparen.cpp
More file actions
46 lines (38 loc) · 854 Bytes
/
Copy pathparen.cpp
File metadata and controls
46 lines (38 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
#define MAXN 3000
#define INF 999999
using namespace std;
int PD[MAXN][MAXN];
string P;
int parentizacao(int i, int n, int parenAberto){
if(i > n){
if(parenAberto == 0){
return 0;
}else{
return INF;
}
}
if(parenAberto < 0){
return INF;
}
if(PD[i][parenAberto] != -1){
return PD[i][parenAberto];
}
if(P[i] == '('){
PD[i][parenAberto] = min(1+parentizacao(i+1, n, parenAberto-1), parentizacao(i+1, n, parenAberto+1));
return PD[i][parenAberto];
}else{
PD[i][parenAberto] = min(1+parentizacao(i+1, n, parenAberto+1), parentizacao(i+1, n, parenAberto-1));
return PD[i][parenAberto];
}
}
int main(){
cin >> P;
for(int i=0; i<(int)P.length()+20; i++){
for(int j=0; j<(int)P.length()+20; j++){
PD[i][j] = -1;
}
}
int result = parentizacao(0, P.length()-1, 0);
cout << result;
}