HDU_ACM_final

Q-Sequence
Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 54    Accepted Submission(s): 6

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
 

Author
Agreal@TJU


#include<iostream>
#include<cstring>
using namespace std;
char * sub_string(char *s,int first_index,int last_index)
{
  int len=strlen(s);
  char *sub=new char[len];
  for( int i=0;i<last_index-first_index;i++ )
   sub[i]=s[first_index+i];
  return sub;
}

bool is_QS(char s[])
{
 int len=strlen(s);
 if( len==1 && s[0]=='0' )
  return true;
 else if(s[len-1]=='1')
 {
  //i---1~len-2
  for(int i=1;i<=len-2;i++)
  { 
   char *sub1=new char[i];
   char *sub2=new char[len-1-i];
   sub1=sub_string(s,len-1-i,len-1-1);//最好动态份配
   sub2=sub_string(s,0,len-1-i-1);
    if( is_QS(sub1) && is_QS(sub2))
    {    
     delete sub1,sub2;
     return true;
    }
    else
    {    delete sub1,sub2;
     return false;
    }
  }
 }
 else
  return false;
}

int main()
{   char s[1001];
    cin>>s;
 //int len=strlen(s);
// cout<<s<<"  "<<endl;
 
 if( is_QS(s) )
  cout<<"Yes"<<endl;
 else
  cout<<"No"<<endl;
 return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值