DSA 祖玛

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

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

Output

ABCCBA
AABCCBA
AABBCCBA
-
A

Restrictions

0 <= n <= 10^4

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

Time: 2 sec

Memory: 256 MB

描述

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

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

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

输入

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

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

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

输出

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

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

样例

见英文题面

限制

0 ≤ n ≤ 10^4

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

时间:2 sec

内存:256 MB

首先,看到插入,没有随机读取,想到要用链表,由于无法使用stl,所以只能自己实现,需要实现初始化,插入,打印,消除函数。

  1 #include <cstdio>
  2 #include <cstring>
  3 const int N = 2e4 + 5;
  4 char s[N];
  5 struct mylist
  6 {
  7     char ch;
  8     mylist *nxt, *pre;
  9     mylist(char c, mylist *x, mylist *y)
 10     {
 11         ch = c;
 12         nxt = x;
 13         pre = y;
 14     }
 15 };
 16 mylist *head = new mylist('-', nullptr, nullptr);
 17 mylist *tail = new mylist('-', nullptr, nullptr);
 18 void init()
 19 {
 20     int len = strlen(s);
 21     if (!len)
 22         return;
 23     mylist *now;
 24     for (int i = 0; i < len; ++i)
 25     {
 26         mylist *p = new mylist(s[i], nullptr, nullptr);
 27         if (!i)
 28             head = p;
 29         else
 30             now->nxt = p, p->pre = now;
 31         now = p;
 32     }
 33     now->nxt = tail;
 34     tail->pre = now;
 35 }
 36 void check(mylist *p)
 37 {
 38     int sum = 1;
 39     mylist *x, *y;
 40     for (x = p->pre; x != nullptr; x = x->pre)
 41     {
 42         if (x->ch == p->ch)
 43             sum ++;
 44         else
 45             break;
 46     }
 47     for (y = p->nxt; y->ch != '-'; y = y->nxt)
 48     {
 49         if (y->ch == p->ch)
 50             sum ++;
 51         else
 52             break;
 53     }
 54     if (sum >= 3)
 55     {
 56         if (x)
 57             x->nxt = y;
 58         else
 59             head = y;
 60         y->pre = x;
 61         if (x && x->ch != '-')
 62             check(x);
 63         else if (y && y->ch != '-')
 64             check(y);
 65     }
 66 }
 67 void insert(int x, char y)
 68 {
 69     mylist *ptr = head;
 70     for (int i = 0; i < x; ++i)
 71         ptr = ptr->nxt;
 72     mylist *p = new mylist(y, ptr, ptr->pre);
 73     if (x)
 74         ptr->pre->nxt = p;
 75     else
 76         head = p;
 77     ptr->pre = p;
 78     check(p);
 79 }
 80 void print()
 81 {
 82     if (head->ch == '-')
 83     {
 84         printf("-
");
 85         return;
 86     }
 87     mylist *p = head;
 88     while (p->ch != '-')
 89     {
 90         printf("%c", p->ch);
 91         p = p->nxt;
 92     }
 93     printf("
");
 94 }
 95 int main()
 96 {
 97     // gets(s);
 98     scanf("%s", s);
 99     init();
100     int q;
101     scanf("%d", &q);
102     while (q--)
103     {
104         int x;
105         char y[3];
106         scanf("%d%s", &x, y);
107         insert(x, y[0]);
108         print();
109     }
110     return 0;
111 }
View Code

首先一个坑点在于有空字符串,题解用的是gets,但是在c++11中被移除了,我也没有想到相对好一些的解决办法

其次是写队列最好找两个哨兵,一前一后,这样所有操作就方便的很,我写的只用了末尾一个哨兵,所以还有一些地方需要讨论开头,很烦。

最后,就是时间复杂度没有分析好。我一开始在63行处没有加else,所以相当于把线性时间复杂度的东西强行变成了指数复杂度,气啊。

原文地址:https://www.cnblogs.com/fantasquex/p/11253199.html