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.
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 }