浪一生 2016-07-23 10:21 采纳率: 0%
浏览 2990

清华oj数据结构问题:祖玛

本地测试数据已经通过了,提交上去就是过不了,我已经尽量在每个可能空指针的地方判断过了,心累

描述
祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干个彩色珠子,其中任意三个相邻的珠子不会完全同色。此后,你可以发射珠子到轨道上并加入原有序列中。一旦有三个或更多同色的珠子变成相邻,它们就会立即消失。这类消除现象可能会连锁式发生,其间你将暂时不能发射珠子。

开发商最近准备为玩家写一个游戏过程的回放工具。他们已经在游戏内完成了过程记录的功能,而回放功能的实现则委托你来完成。

游戏过程的记录中,首先是轨道上初始的珠子序列,然后是玩家接下来所做的一系列操作。你的任务是,在各次操作之后及时计算出新的珠子序列。

输入
第一行是一个由大写字母'A'~'Z'组成的字符串,表示轨道上初始的珠子序列,不同的字母表示不同的颜色。

第二行是一个数字n,表示整个回放过程共有n次操作。

接下来的n行依次对应于各次操作。每次操作由一个数字k和一个大写字母Σ描述,以空格分隔。其中,Σ为新珠子的颜色。若插入前共有m颗珠子,则k ∈ [0, m]表示新珠子嵌入之后(尚未发生消除之前)在轨道上的位序。

输出
输出共n行,依次给出各次操作(及可能随即发生的消除现象)之后轨道上的珠子序列。

如果轨道上已没有珠子,则以“-”表示。

样例
Input

ACCBA
5
1 B
0 A
2 B
4 C
0 A
Output

ABCCBA
AABCCBA

AABBCCBA

A

限制
0 ≤ n ≤ 10^4

0 ≤ 初始珠子数量 ≤ 10^4

时间:2 sec

内存:256 MB

提示
列表

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; 

#define pNode Node*
#define pList List*

const int SZ = 1<<20;   
struct fastio{   //fast io
    char inbuf[SZ];  
    char outbuf[SZ];  
    fastio(){  
        setvbuf(stdin,inbuf,_IOFBF,SZ);  
        setvbuf(stdout,outbuf,_IOFBF,SZ);  
    }  
}io;  

struct Node {
        char color;
        pNode pred;
        pNode succ;

        Node() {
        }

        Node(char c,pNode pred=NULL,pNode succ=NULL):color(c),pred(pred),succ(succ) {

        }

        void insertAsSucc(char c);

        void insertAsPred(char c); 
}; 
void Node::insertAsSucc(char c) {
    pNode x = new Node(c,this,this->succ);
    this->succ = x;
}

void Node::insertAsPred(char c) {
    pNode x = new Node(c,this->pred,this);
    this->pred = x;
}
class List {
    private:
        int size;
        pNode head;
        pNode trai;

    public:
        List() {
            head = new Node();
            trai = new Node();
            head->succ = trai;
            trai->pred = head;
            head->color = '-';
            trai->color = '^';
            size = 0;
        }

        pNode insert(int n,char c);
        void traverseShow();
        void insertBfTrai(char c);
        void check(pNode start); 
        pNode checkB(pNode p);
        pNode checkA(pNode p,bool &change);
        pNode remove(pNode p); 
};
pNode List::remove(pNode p) {  //规定删除后返回前一节点 
    if(p == this->trai) return p->pred;
    if(p == this->head) return p;
    pNode q = p->pred;
    p->pred->succ = p->succ;
    p->succ->pred = p->pred;
    delete p;
    size--;
    return q;
}
void List::check(pNode start) {
    bool change = true;  //以下循环的flag 
    while(change) {
        start = checkB(start);
        start = checkA(start,change);   
    }
}
pNode List::checkB(pNode p) {  //从p开始往前(before) 检查 
    if(p==this->head) return p->succ; //每次有->pred,->succ前都要检查 
    pNode q = p->pred;
    int count = 1;
    while(q!=this->head && q->color==p->color) { 
        q=q->pred;
        count ++;

        if(count == 3) {
            for(int i=0;i<3;i++) {
                p = this->remove(p);
            }
            count=1;
            if(p==this->head) return p->succ;
            q=p->pred;
        }
    }
    return q->succ;  //q和p内容不同,q->succ和p内容相同 
}
pNode List::checkA(pNode p,bool &change) { //从p开始往后(After)检查 
    change = false; 
    if(p==this->trai) return p->pred;
    pNode q = p->succ;
    int count = 1;
    while(q!=this->trai && q->color==p->color) {
        count++;
        q = q->succ;

        if(count == 3) {  //可以消掉 
            change = true;   //外层循环 可以继续 
            for(int i=0;i<3;i++) {
                p = this->remove(p);
                p = p->succ;
            }
            count = 1;
            if(p==this->trai) return p->pred;
            q = p->succ;
        }
    }
    return q->pred;
}
void List::insertBfTrai(char c) {  //在尾节点前插入一个节点 
    pNode p = new Node(c,trai->pred,trai);
    trai->pred->succ = p;
    trai->pred = p;

    size++;
}
pNode List::insert(int n,char c) {  //从第n个位置插入 
    if(n>this->size) n=this->size;
    pNode p = head;
    while(n-- > 0) {
        if(p != this->trai)
            p=p->succ;
    }
    p->insertAsSucc(c); 
    size++;

    return p->succ;
}
void List::traverseShow() { //输出整个链表 内容 

    pNode p = head;
    if(this->size == 0) printf("-");
    while((p=p->succ)!=trai) {
        printf("%c",p->color);
    }
    printf("\n");
}

//相关全局量定义
char s[20000];   //<10^4即可 
int posi[20000];
char colors[20000];

int main(){
    #ifndef _OJ_
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    //data input
    gets(s);
    int len=strlen(s);
    int n;   //insert的操作次数 
    scanf("%d",&n);
    //printf("%d ",len);

    pList balls = new List();
    for(int i=0;i<len;i++) {
        if('A'<=s[i] && s[i]<='Z')
            balls->insertBfTrai(s[i]);   //初始状态构建 
    }

    for(int i=0;i<n;i++) {
        scanf("%d %c",posi+i,colors+i);  //n行插入操作输入 
    }
    //执行n次操作 
    for(int i=0;i<n;i++) {
        if('A'<=colors[i] && colors[i]<='Z') {
            pNode p = balls->insert(posi[i],colors[i]); //取得新插入节点 
            balls->check(p);   //每插入一个数据,就从插入点开始检查能否消掉 
            balls->traverseShow();  //输出 
        }   
    }

    delete balls;
    return 0;
}

图片说明

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-07-23 15:53
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误