邓俊辉老师数据结构--zuma编程题

天大地大妈咪最大 提交于 2019-11-27 07:31:10

题目:https://dsa.cs.tsinghua.edu.cn/oj/problem.shtml?id=1143
第一次提交95,居然是case2错了,
仔细检查一下,发现创建链表出错了,没有考虑开始时是空链表的情况,检查完后就ok了。
100分代码如下,

#include <cstdio>
#include<string.h>

using namespace std;

struct zuma{
    char a;
    zuma* pre;
    zuma* next;
}*head,*tail,*pos;

int reduce(char a1){
    if(pos==tail)return 0;
    zuma *p=pos,*q=pos,*r;
    int i=0,j=0;
    while(p->pre&&p->pre->a==a1){p=p->pre;i++;}
    while(q->next&&q->next->a==a1){q=q->next;j++;}
    if(i+j>1){
        r=p;
        p=p->pre;
        q=q->next;
        p->next=q;
        q->pre=p;
        if(p!=head)pos=p;
        else pos=p->next;
        while(r!=q){p=r;r=r->next;if(p!=head||p!=tail)delete(p);}
        return 1;
    }
    return 0;
}
int main()
{
    zuma *p,*previous;
    head=new zuma;
    tail=new zuma;
    head->a='0';
    tail->a='0';
    head->pre=NULL;
    head->next=tail;
    tail->pre=head;
    tail->next=NULL;

    char aa;
    previous=head;
    while(scanf("%c",&aa),aa>='A'&&aa<='Z'){
        p=new zuma;
        p->a=aa;
        p->next=previous->next;
        previous->next->pre=p;
        previous->next=p;
        p->pre=previous;
        previous=p;
    }

    int n,b;
    char c;
    scanf("%d",&n);
    while(n--){
        scanf("%d %c",&b,&c);
        p=new zuma;
        previous=head;
        p->a=c;
        int bb=b;
        while(bb--){
            previous=previous->next;
        }
        p->next=previous->next;
        previous->next->pre=p;
        previous->next=p;
        p->pre=previous;
        pos=p;
        while(reduce(pos->a)&&head->next!=tail);
        if(head->next==tail){
            puts("-");
        } else{
            p=head->next;
            while(p!=tail){
                printf("%c",p->a);
                p=p->next;
            }
            printf("\n");
        }
    }
    return 0;
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!