Alex’s Game(I)

来源:互联网 发布:博奥软件官网 编辑:程序博客网 时间:2024/06/15 23:36
F - Alex’s Game(I)
Time Limit:1000MS    Memory Limit:65535KB    64bit IO Format:%I64d & %I64u
SubmitStatus

Description

Alex likestoplay 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.

Output

For each case output "YES" in a single line if it’s legal.
Or you need to output “NO”;

Sample Input

3010101100011

Sample Output

YESNOYES题意:往一个空序列中插入字符串01,问输入的字符串是否满足要求思路:因为不管怎么插入0总是在1前面插入,所以只要按照顺序计算0和1的个数就行了。在计算时比较0和1的个数,如果1的个数大于0的个数,就不符合要求。
#include<stdio.h>#include<string.h>int main(){    char ch[1005];    int n;    scanf("%d",&n);    while(n--)    {        scanf("%s",ch);        int len=strlen(ch);        int s0=0,s1=0;        int flag=1;        for(int i=0;i<len;i++)        {            if(ch[i]=='0')                s0++;            else                s1++;            if(s1>s0)            {                flag=0;                break;            }        }        if(flag==0)            printf("NO\n");        else            printf("YES\n");    }    return 0;}


0 0
原创粉丝点击