2021-03-27

问题 D: DS双向链表—祖玛

时间限制: 1 Sec  内存限制: 128 MB
提交: 211  解决: 85
[提交][状态][讨论版]

题目描述

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

给定轨道上初始的珠子序列,然后是玩家所做的一系列操作。你的任务是,在各次操作之后及时计算出新的珠子序列。

 

输入

第一行是一个数字n,表示接下来输入n个字符串。

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

第三行是一个数字t,表示玩家共有t次操作。

接下来的t行依次对应于各次操作。每次操作由一个数字k和一个大写字母描述,以空格分隔。其中,大写字母为新珠子的颜色。若插入前共有m颗珠子,位置0-m-1,则k ∈ [0, m]表示新珠子嵌入在轨道上的位置。

输出

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

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

样例输入

5

ACCBA

5

1 B

0 A

2 B

4 C

0 A

样例输出

ABCCBA

AABCCBA

AABBCCBA

-

A

#include<iostream> 
using namespace std;

class listnode {
public:
	char data;
	listnode *prior;
	listnode *next;
	listnode() {
		prior = NULL;
		next = NULL;
		data = '\0';
	}
};

class list {
public:
	listnode *head;
	int len;
	list() {
		head = new listnode();
		len = 0;
	}
	~list() {
		listnode *q = head;
		while (q != NULL) {
			listnode *p = q->next;
			delete q;
			q = p;
		}
	}
	listnode *index(int i) {
		listnode *c;
		c = head;
		for (int j = 0; j < i; j++) {
			c = c->next;
		}
		return c;
	}
	void insert(int i, char ch) {
		listnode *c = index(i);
		listnode *q;
		listnode *p = NULL;
		if (c->next != NULL) {
			p = c->next;
		}
		c->next = new listnode();
		c->next->data = ch;
		c->next->next = p;
		c->next->prior = c;
		if (p != NULL) {
			p->prior = c->next;
		}
		len++;
		q = c->next;
		listnode *x = find(q);
		while (x != NULL) {
			x = find(x);
		}
	}
	listnode *find(listnode *c) {
		listnode *p = c->prior;
		listnode *n = c->next;
		int flag = 1;
		while (p != NULL && p->data == c->data) {
			p = p->prior;
			flag++;
		}
		while (n != NULL && n->data == c->data) {
			n = n->next;
			flag++;
		}
		if (flag >= 3) {
			p = c->prior;
			n = c->next;
			while (p != NULL && p->data == c->data) {
				listnode *p1 = p->prior;
				del(p);
				p = p1;
				
			}
			while (n != NULL && n->data == c->data) {
				listnode *n1 = n->next;
				del(n);
				n = n1;
			}
			listnode *l = c->next != NULL ? c->next : c->prior;
			if (l == head)l = NULL;
			del(c);
			return l;
		}
		return NULL;
	}
	void del(listnode *c) {
		listnode *p = c->prior;
		listnode *n = c->next;
		if (p != NULL) p->next = n;
		if (n != NULL) n->prior = p;
		delete c;
	}
	void display() {
		listnode *c = head;
		if (c->next == NULL) {
			cout << "-" << endl;
		}
		else {
			listnode *p = c->next;
			while (1) {
				if (p == NULL) break;
				cout << p->data;
				p = p->next;
			}
			cout << endl;
		}
	}
};
int main() {
	int n;
	list my;
	char ch;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> ch;
		my.insert(i, ch);
	}
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int num;
		cin >> num >> ch;
		my.insert(num, ch);
		my.display();
	}
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值