#include <bits/stdc++.h>
using namespace std;
int minSteps(string& S, int i, int count1, int count2,
int first, int second, int n)
{
if (i == n) {
return 0;
}
if (count1 > (second + count2)
|| count2 > (first + count1)) {
return 0;
}
int c1 = 0, c2 = 0;
if (i % 2 == 0) {
if (S[i] == '?' ) {
return min(
1
+ minSteps(
S, i + 1,
count1 + 1, count2,
first - 1, second, n),
1
+ minSteps(
S, i + 1, count1, count2,
first - 1, second, n));
}
else if (S[i] == '1' ) {
c1 = 1
+ minSteps(
S, i + 1,
count1 + 1, count2,
first - 1, second, n);
return c1;
}
else {
c2 = 1
+ minSteps(
S, i + 1,
count1, count2,
first - 1, second, n);
return c2;
}
}
else {
if (S[i] == '?' ) {
return min(
1
+ minSteps(
S, i + 1,
count1, count2 + 1,
first, second - 1, n),
1
+ minSteps(
S, i + 1,
count1, count2,
first, second - 1, n));
}
else if (S[i] == '1' ) {
c1 = 1
+ minSteps(
S, i + 1,
count1, count2 + 1,
first, second - 1, n);
return c1;
}
else {
c2 = 1
+ minSteps(
S, i + 1, count1,
count2, first,
second - 1, n);
return c2;
}
}
}
int main()
{
string s = "?10?0?" ;
int N = s.size();
cout << minSteps(s, 0, 0, 0,
N / 2, N / 2, N);
return 0;
}
|