开发商最近准备为玩家写一个游戏过程的回放工具。他们已经在游戏内完成了过程记录的功能,而回放功能的实现则委托你来完成。
游戏过程的记录中,首先是轨道上初始的珠子序列,然后是玩家接下来所做的一系列操作。你的任务是,在各次操作之后及时计算出新的珠子序列。
第二行是一个数字n,表示整个回放过程共有n次操作。
接下来的n行依次对应于各次操作。每次操作由一个数字k和一个大写字母Σ描述,以空格分隔。其中,Σ为新珠子的颜色。若插入前共有m颗珠子,则k ∈ [0, m]表示新珠子嵌入之后(尚未发生消除之前)在轨道上的位序。
如果轨道上已没有珠子,则以“-”表示。
ACCBA
5
1 B
0 A
2 B
4 C
0 A
Output
ABCCBA
AABCCBA
AABBCCBA
0 ≤ 初始珠子数量 ≤ 10^4
时间:2 sec
内存:256 MB
思路感觉不难,用双向链表表示,然后每次插入后对相邻的颜色进行比对消除,需要注意的是在第一次消除过后可能会引起连锁消除,特别是AABBAA的类型在插入B后会发生四个的消除。因此需要在消除一次后将控制权交给相邻并且存在的柱子即可。
#include<iostream> #define MAXSIZE 10000 using namespace std; char s[MAXSIZE]; char x; int n, len, num = 0; int maga = 0; struct node { char color; node* pre; node* next; }*head, * tail, * k1, * k2; void insert(int a, char k) { node* n1 = head->next; node* n2 = new node; n2->color = k; if (a == 0 && head->next == tail) { n2->pre = head; head->next = n2; n2->next = tail; tail->pre = n2; maga++; } else { while (a-- > 0) { n1 = n1->next; } n2->pre = n1->pre; n2->next = n1; n1->pre->next = n2; n1->pre = n2; maga++; } while (maga >= 3) { if (n2->pre->color == n2->color && n2->color == n2->next->color && n2->next->color == n2->next->next->color) { n2->next->next->next->pre = n2->pre->pre; n2->pre->pre->next = n2->next->next->next; maga = maga - 4; if (n2->pre->pre->color != '#') { n2 = n2->pre->pre; } else { n2 = n2->next->next->next; } } else if (n2->color == n2->pre->color && n2->pre->color == n2->next->color) { n2->pre->pre->next = n2->next->next; n2->next->next->pre = n2->pre->pre; maga = maga - 3; if (n2->pre->pre->color != '#') { n2 = n2->pre->pre; } else { n2 = n2->next->next; } } else if (n2->color == n2->pre->color && n2->pre->color == n2->pre->pre->color) { n2->pre->pre->pre->next = n2->next; n2->next->pre = n2->pre->pre->pre; maga = maga - 3; if (n2->pre->pre->pre->color != '#') { n2 = n2->pre->pre->pre; } else { n2 = n2->next; } } else if (n2->color == n2->next->color && n2->next->color == n2->next->next->color) { n2->next->next->next->pre = n2->pre; n2->pre->next = n2->next->next->next; maga = maga - 3; if (n2->pre->color != '#') { n2 = n2->pre; } else { n2 = n2->next->next->next; } } else { break; } } } void init() { head = new node; tail = new node; head->color = '#'; head->next = tail; head->pre = NULL; tail->color = '#'; tail->next = NULL; tail->pre = head; for (int i = 0; i < len; i++) { k1 = new node; k1->color = s[i]; if (head->next == tail) { k1->pre = head; head->next = k1; k1->next = tail; tail->pre = k1; } else { k1->next = tail; k1->pre = tail->pre; tail->pre->next = k1; tail->pre = k1; } maga++; } } int main() { gets_s(s); while (s[len] >= 'A' && s[len] <= 'Z') len++; scanf("%d", &n); init(); while (n-- > 0) { scanf("%d %c", &num, &x); if (x >= 'A' && x <= 'Z') { insert(num, x); } if (maga == 0) { printf("%c", '-'); } else { k2 = head->next; for (int i = 0; i < maga; i++) { printf("%c", k2->color); k2 = k2->next; } } printf("\n"); } }
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/85206.html