Tsinghua OJ 数据结构编程作业:祖玛(Zuma)

来源:互联网 发布:部落冲突镜像药水数据 编辑:程序博客网 时间:2024/06/01 10:33

Description

Let's play the game Zuma!

There are a sequence of beads on a track at the right beginning. All the beads are colored but no three adjacent ones are allowed to be with a same color. You can then insert beads one by one into the sequence. Once three (or more) beads with a same color become adjacent due to an insertion, they will vanish immediately.

Note that it is possible for such a case to happen for more than once for a single insertion. You can't insert the next bead until all the eliminations have been done.

Given both the initial sequence and the insertion series, you are now asked by the fans to provide a playback tool for replaying their games. In other words, the sequence of beads after all possible eliminations as a result of each insertion should be calculated.

Input

The first line gives the initial bead sequence. Namely, it is a string of capital letters from 'A' to 'Z', where different letters correspond to beads with different colors.

The second line just consists of a single interger n, i.e., the number of insertions.

The following n lines tell all the insertions in turn. Each contains an integer k and a capital letter Σ, giving the rank and the color of the next bead to be inserted respectively. Specifically, k ranges from 0 to m when there are currently m beads on the track.

Output

n lines of capital letters, i.e., the evolutionary history of the bead sequence.

Specially, "-" stands for an empty sequence.

Example

Input

ACCBA51 B0 A2 B4 C0 A

Output

ABCCBAAABCCBAAABBCCBA-A

Restrictions

0 <= n <= 10^4

0 <= length of the initial sequence <= 10^4

Time: 2 sec

Memory: 256 MB

Hints

List

描述

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

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

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

输入

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

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

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

输出

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

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

样例

见英文题面

限制

0 ≤ n ≤ 10^4

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

时间:2 sec

内存:256 MB

提示

列表

#1 使用STL list

题目已提示使用list,首先贴上使用STL的代码,由于Tsinghua OJ上禁掉了大部分STL,所以只测试了样例中的数据。

#include <stdio.h>#include <iostream>#include <list>using namespace std;#define N 20001list<char> l('#');char s[N];void del( list<char>::iterator in, char c){int sum = 0;list<char>::iterator first, second = in;-- in;first = in;for (; first != l.begin(); -- first ) {if (*first == c) {sum ++;}else break;}for (; second != l.end(); ++ second) {if (*second == c && c != '\0') {sum ++;}else break;}if (sum >= 3) {l.erase(++first, second);del(second, *second);}}int main(){scanf("%s",&*s);int len = (int)strlen(s);for (int i = len-1; i >=0 ; i --) {l.push_front(s[i]);}l.push_front('#');int n;scanf("%d",&n);for (int i = 0; i < n; i ++) {int ind;char c;scanf("%d %c",&ind,&c);list<char>::iterator in = ++l.begin();for (int j = 0; j < ind && in!=l.end() ; j ++) {++ in;}l.insert(in, c);del( in, c);list<char>::iterator p = ++ l.begin();if (*p == '\0') {printf("-\n");}else {for (; p != l.end(); p ++) {printf("%c",*p);}printf("\n");}}    return 0;}


#2 使用结构体编写双向链表

既然STL不能用,我们只能自己写一个双向链表的结构,代码上传之后,最后一个测试不能通过,第二个测试没有对输入空串做处理。暂且贴出代码如下:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;#define N 20001const int SZ = 1<<20;  //快速iostruct fastio{char inbuf[SZ];char outbuf[SZ];fastio(){setvbuf(stdin,inbuf,_IOFBF,SZ);setvbuf(stdout,outbuf,_IOFBF,SZ);}}io;typedef struct node{char data;struct node *next;struct node *front;}List, *l;l pHead = (l)malloc(sizeof(List));l pTail = (l)malloc(sizeof(List));void creat(char *a, int n){l pt = pHead;//初始化pHead和pTail两个结点,pt为游标,指向当前插入结点pTail->front = pHead;pTail->next = NULL;pHead->front = NULL;pHead->next = pTail;pHead->data = pTail->data = '-';//在头和尾插入数据for (int i = 0; i < n; i ++) {l pNew = (l)malloc(sizeof(List));pNew->data = a[i];pNew->front = pt;pNew->next = pt->next;pt->next->front = pNew;pt->next = pNew;pt = pNew;}}void insert(int po, char c){l pt = pHead, pNew = (l)malloc(sizeof(List));for (int i = 0; i <= po; i ++) {pt = pt->next;}pNew->data = c;pNew->next = pt;pNew->front = pt->front;pt->front->next = pNew;pt->front = pNew;}void del(l pt){int sum = 1;l first, second;first = pt->front;second = pt->next;for (; first != pHead; first = first->front ) {if (first->data == pt->data) {sum ++;}else break;}for (; second != pTail; second = second->next) {if (second->data == pt->data) {sum ++;}else break;}if (sum >= 3) {for (l i = first->next; i != second; i = first->next) {l temp = i;first->next = temp->next;temp->next->front = first;delete temp;}if (second != pTail) {del(second);}else if(first != pHead){del(first);}}}char s[N];int main(){scanf("%s",&*s);int len = (int)strlen(s);creat(s, len);int n;scanf("%d",&n);for (int i = 0; i < n; i ++) {int ind;char c;scanf("%d %c",&ind,&c);insert(ind, c);l pt = pHead;for (int i = 0; i <= ind; i ++) {pt = pt->next;}del(pt);pt = pHead->next;if (pt == pTail) {printf("-\n");}else {for (; pt != pTail; pt = pt->next) {printf("%c",pt->data);}printf("\n");}}    return 0;}


#3 使用字符数组

第三个方法是参考别人的,使用了字符数组,使用了strcpy,过程简单,一看就懂,而且用时也较短,通过所有测试,代码如下:

#include <stdio.h>#include <stdlib.h>#include <cstring>#include <iostream>using namespace std;const int SZ = 1<<20;  //快速iostruct fastio{char inbuf[SZ];char outbuf[SZ];fastio(){setvbuf(stdin,inbuf,_IOFBF,SZ);setvbuf(stdout,outbuf,_IOFBF,SZ);}}io;char s[20001];char temp[20001];int len = 0, po;int del(int p){int first = p,second = p;char elem = s[p];while(s[first] == elem && first) first--;if(first || s[first] != elem) first++;while(s[second] == elem && second < len) second++;if(second - first > 2){strcpy(temp,s + second);strcpy(s + first, temp);len = len + first - second;po = first;return 1;}elsereturn 0;}int main(){gets(s);while(s[len] >= 'A' && s[len] <= 'Z') len++;int n;scanf("%d",&n);for (int i = 0; i < n; i ++) {char c;scanf("%d %c",&po,&c);strcpy(temp, s + po);strcpy(s + po + 1, temp);s[po] = c;len ++;while(del(po) && len);if(len)puts(s);elseputs("-");}    return 0;}




0 0