Q-Sequence

Q-Sequence

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 672    Accepted Submission(s): 334


Problem Description
A Q-sequence is defined as:

Q-Seq := 0 or
Q-Seq := Q-seq Q-seq 1

That is to say a Q-Sequence is a single '0' or two Q-Sequences followed by an '1'.

Given a sequence of '0's and '1's, you are to determine whether it is a Q-Sequence.
 

 

Input
The first line is a number n refers to the number of test cases. Then n lines follows, each line has a string made up of '1's and '0's. The maximum length of the sequence is 1000.
 

 

Output
The output contain n lines, print "Yes" if it is a Q-sequence, otherwise print "No".
 

 

Sample Input
3
0010011
0101
00011
 

 

Sample Output
Yes
No
Yes
一开始还以为要递归呢,没想出来,后来想了一下,真的水。
只要计算0和1的个数,最后判断下就差不多了,具体思路看代码。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <set>
 6 #define mem(a) memset(a,0,sizeof(a))
 7 using namespace std;
 8 char s[1005];
 9 
10 void test(){
11     cout<<"**"<<endl;
12 }
13 int main(){
14     int n;
15     scanf("%d",&n);
16     while(n--){
17         mem(s);
18         scanf("%s",&s);
19         int len=strlen(s);
20         int cnt=0;
21         bool prime=true;
22         for(int i=0;i<len;i++){
23            // test();
24             if(s[0]==1){
25                 prime=false;
26                 break;
27             }
28             if(s[i]=='0')
29                 cnt++;
30             else if(s[i]=='1'&&cnt>1){
31                 cnt--;
32             }else if(s[i]=='1'&&cnt==1){
33                // test();
34                 prime=false;
35                 break;
36             }
37         }
38         if(prime&&cnt==1){
39             cout<<"Yes"<<endl;
40         }else
41             cout<<"No"<<endl;
42     }
43     return 0;
44 }

 

 

转载于:https://www.cnblogs.com/zllwxm123/p/7341803.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值