#include<stdio.h>
#include<string.h>
#define INF 1 << 30;
int a[25],cnt[25];
int len,temp,ans;
int OK()
{
int i;
for(i = 0;i < len;i ++)
if(a[i])
return 0;
return 1;
}
void dfs(int index)
{
int i;
if(OK())
{
temp = 0;
for(i = 0;i < len;i ++)
if(cnt[i] == 1)
temp++;
if(temp < ans)
ans = temp;
return ;
}
if(index >= len)
return ;
for(cnt[index] = 0;cnt[index] < 2; )
{
a[index] ^= 1;
if(index > 0)
a[index-1] ^= 1;
if(index < len-1)
a[index+1] ^= 1;
cnt[index]++;
dfs(index+1);
}
}
int main()
{
int i;
char str[22];
while(~scanf("%s",str))
{
len = strlen(str);
for(i = 0;i < len;i ++)
a[i] = str[i]-'0';
ans = INF;
memset(cnt,0,sizeof(cnt));
dfs(0);
if(ans!= 1<<30)
printf("%d\n",ans);
else
printf("NO\n");
}
return 0;
}
转载于:https://www.cnblogs.com/wangzhili/p/3950371.html