#include <bits/stdc++.h> using namespace std; typedef long long int ll; int kon[20]; int nr[20]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int k,n,l,p,best=999999999,odp,depth; string a; cin >> n >> k; cin >> a; for (int mask=0;mask<(1<<n);mask++){ if (__builtin_popcount(mask)!=k-1){ continue; } nr[0] = 1; odp = 0; for (int x=0;x<n;x++){ nr[x+1] = nr[x]; if (mask & (1<<x)){ nr[x+1] += 1; } } for (int x=0;x<n;x++){ for (int y=x+1;y<n;y++){ depth = 0; if (nr[x]!=nr[y]){ continue; } for (int z=x;z<=y;z++){ if (a[z] == '('){ depth += 1; } else{ depth -= 1; } if (depth < 0){ depth = -2137; break; } } if (depth==0){ odp += 1; } } } best = min(best,odp); } cout << best << '\n'; return 0; }
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 47 48 49 50 51 52 53 54 55 | #include <bits/stdc++.h> using namespace std; typedef long long int ll; int kon[20]; int nr[20]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int k,n,l,p,best=999999999,odp,depth; string a; cin >> n >> k; cin >> a; for (int mask=0;mask<(1<<n);mask++){ if (__builtin_popcount(mask)!=k-1){ continue; } nr[0] = 1; odp = 0; for (int x=0;x<n;x++){ nr[x+1] = nr[x]; if (mask & (1<<x)){ nr[x+1] += 1; } } for (int x=0;x<n;x++){ for (int y=x+1;y<n;y++){ depth = 0; if (nr[x]!=nr[y]){ continue; } for (int z=x;z<=y;z++){ if (a[z] == '('){ depth += 1; } else{ depth -= 1; } if (depth < 0){ depth = -2137; break; } } if (depth==0){ odp += 1; } } } best = min(best,odp); } cout << best << '\n'; return 0; } |