Problem
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
in
ACCBA
5
1 B
0 A
2 B
4 C
0 A
out
ABCCBA
AABCCBA
AABBCCBA
-
A
Restrictions
0 <= n <= 10^4
0 <= length of the initial sequence <= 10^4
Time: 2 sec
Memory: 256 MB
Solution
祖马的游戏规则就是同色相邻球达到3个或以上可以把这些球全部消掉。
每次插入新球的时候,只在插入的地方有可能会出现3个同色相邻球,就把它们消掉,不过要注意消掉之后还可能产生新的同色相邻球,要继续处理,直到目前的串全部被消完或者是不能继续消了。
//
// Created by DongChenglong on 2020/4/1.
//
#include <stdio.h>
#include <stdlib.h>
struct node {
char ch;
struct node *pred;
struct node *succ;
};
void init(struct node *head, struct node *tail, char *str, int len) {
head->pred = NULL;
head->succ = tail;
tail->pred = head;
tail->succ = NULL;
struct node *p = head;
for (int i = 0; i < len; ++i) {
struct node *x = (struct node *) malloc(sizeof(struct node));
x->ch = *(str + i);
x->pred = p;
x->succ = p->succ;
p->succ->pred = x;
p->succ = x;
p = p->succ;
// printf("%ch\n",p->ch);
}
}
void solution(struct node *p, struct node *head, struct node *tail) {
while (true) {
int cnt = 1;
char c = p->ch;
struct node *lq;
struct node *rq;
for (lq = p->pred; lq != head; lq = lq->pred) {
if (lq->ch == c) cnt++;
else break;
}
for (rq = p->succ; rq != tail; rq = rq->succ) {
if (rq->ch == c) cnt++;
else break;
}
if (cnt >= 3) {
lq->succ = rq;
rq->pred = lq;
if (lq != head) p = lq;
else if (rq != tail) p = rq;
else return;
} else {
break;
}
}
return;
}
void pri(struct node *head, struct node *tail) {
if (head->succ == tail) {
printf("-\n");
return;
}
for (struct node *p = head->succ; p != tail; p = p->succ) {
printf("%c", p->ch);
}
printf("\n");
return;
}
int main() {
char str[10005];
int len = 0;
char c;
fflush(stdin);
while (true) {
c=getchar();
if (c=='\n')
break;
else
str[len++] = c;
}
// puts(str);
struct node *head = (struct node *) malloc(sizeof(struct node));
struct node *tail = (struct node *) malloc(sizeof(struct node));
init(head, tail, str, len);
int n;
scanf("%d", &n);
while (n--) {
int pos;
char c;
scanf("%d %c", &pos, &c);
struct node *p = head;
for (int i = 0; i < pos; ++i) //后插入
{
p = p->succ;
}
struct node *x = (struct node *) malloc(sizeof(struct node));
x->ch = c;
x->pred = p;
x->succ = p->succ;
p->succ->pred = x;
p->succ = x;
p = p->succ;
solution(p, head, tail);
pri(head, tail);
}
// for (struct node *p=head->succ;p!=tail;p=p->succ)
// {
// printf("%ch\n",p->ch);
// }
return 0;
}