hdu 1854 Q-Sequence

    本题描述参考hdu,在这就不叙述了。下面讲讲我的思路和代码:
    刚开始的时候,用DFs去做,感觉到有一点难度。因为它的回溯条件不好掌握,所以尝试了很久,反而没有结果。不过用指针去做就不一样了,凡是把字符串中"001"换成"0"在回溯就好多了。然后我们判断如果“只”剩下一个"0"那么就输出"Yes",否则就输出"No"。下面是代码:
#include<stdio.h>
#include<string.h>
#include<malloc.h>

struct ch{
char k;
ch *next,*fa;
};

struct ch *root;

void init(ch *&t){
t=(ch *)malloc(sizeof(ch));
t->next=NULL;
t->fa=NULL;
}

char str[1005];

void deal(){
int NumberTrue=0;//记录前面有多少个"0"
struct ch *t=root,*h;
while(t->next!=NULL){
t=t->next;
if(t->k=='0'){
NumberTrue++;
}
else
{
if(NumberTrue>=2){//出现了"001"的情况,变成"0"
NumberTrue--;
h=t->fa->fa;
if(t->next!=NULL)
t->next->fa=h;
h->next=t->next;
}
else
break;
}
}
if(root->next->k=='0' && root->next->next==NULL)//只剩下"0"
printf("Yes\n");
else
printf("No\n");
free(root);
}
void make(){
scanf("%s",str);
init(root);
struct ch *t=root;
int i;
int n=strlen(str);
for(i=0;i<n;i++){
struct ch *h;
init(h);
h->k=str[i];
h->fa=t;
t->next=h;
t=h;
}
t->next=NULL;
deal();
}
int main(){
    int t;
scanf("%d",&t);
while(t--){
make();
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值