Description
Alex likesto play with one and zero!One day he gets an empty string.So our cute boy wants to add one and zero in it. Every time he will add ‘01’in the string at any position and then get a new string.For example:if the string is “01” now ,he can get “0101” or “0011,Now give you a string that is Alex has get,you need to answer whether the string is legal?
Input
First is a integer n(n<=100)
Next contains n lines .Every line is a string whose legth is no more than 1000.
Next contains n lines .Every line is a string whose legth is no more than 1000.
Output
For each case output "YES" in a single line if it’s legal.
Or you need to output “NO”;
Or you need to output “NO”;
Sample Input
3 0101 0110 0011
Sample Output
YES NO YES
Hint
无
这个题很简单,对于字符串里的所有的0,只要0前面的1的个数不会大于0的个数就可以说明所有插入的字符串都是01排列了。
#include<stdio.h> #include<string.h> int main() { int n; char a[1005]; scanf("%d",&n); while(n--) { int flag=0; scanf("%s",a); for(int j=0; j<strlen(a); j++) { if(a[j]=='0') { int sum1=0,sum2=0; for(int k=0; k<j; k++) { if(a[k]=='1') sum1++; else sum2++; } if(sum1>sum2) { printf("NO\n"); flag=1; break; } } } if(!flag) printf("YES\n"); } return 0; }