南邮 OJ 1965 GAME

来源:互联网 发布:linux select串口 编辑:程序博客网 时间:2024/06/04 23:26

GAME

时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 54            测试通过 : 18 

比赛描述

There is a string which consists 0s and 1s only.
Each time you can change one digit, with 0 changed to 1, or 1 changed to 0.
But when you change the digit, the digits on its left and right is changing in the same way.
Please tell me the minimum moves to change the string to 00…000.
If it is impossible please output “NO” in a single line.

输入

In each line there is a string. The length of the string is shorter than 20.

输出

For each string you have to output the minimum number or “NO” in a single line.

样例输入

01
011
101

样例输出

NO
1
2

题目来源

2B




#include<iostream>using namespace std;#define MAX_LEN 21char a[MAX_LEN],b[MAX_LEN];void reverse(char *c, int i){for(int j=max(0,i-1);j<=i+1;j++){if(c[j]=='0'){c[j] = '1';}else if(c[j]=='1'){c[j] = '0';}}}int main(){int i,len,count,r;while(scanf("%s",a)==1){len = (int)strlen(a);strcpy(b,a);count = 0;for(i=0; i+1<len; i++){if(a[i]=='1'){reverse(a,i+1);count++;}}if(a[len-1]=='1'){r = -1;}else{r = count;}count = 1;reverse(b,0);for(i=0; i+1<len; i++){if(b[i]=='1'){reverse(b,i+1);count++;}}if(b[len-1]=='0'){if(r==-1 || r>count){r = count;}}if(r==-1){printf("NO\n");}else{printf("%d\n",r);}}}



0 0
原创粉丝点击