祖玛(Zuma)

IT教程 11个月前 https://55wd.com
1,331

祖玛

祖玛(Zuma)

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.

祖玛(Zuma)

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

  1. 原理与要点:用一个字符串来模拟插入和删除的操作,插入时直接将其放在要插入的位置,然后把后面的后移一位。删除时扫描整个字符串,出现三个及以上连续的便删掉,为了优化删除的时间复杂度,用了一个栈做辅助。
  2. 遇到的问题:
  3. 时间和空间复杂度: 一次插入时间复杂度\(O(n)\),一次删除时间复杂度\(O(n)\),总体时间复杂度\(O(n^2)\),空间复杂度\(O(n)\)
  4. 特别或创新:用一个栈做辅助,使得删除时只需遍历字符串一遍。
#include "iostream" #include "cstdio" #include "cstring" using namespace std; const int maxn = 2e4 + 100; bool vis[maxn]; char str[maxn]; int m, pos; struct no { int num; char ch; } st[maxn]; void ins(int pos, char op) { int len = strlen(str + 1); str[len + 2] = '\0'; for (int i = len + 1; i > pos + 1; i--) { str[i] = str[i - 1]; } str[pos + 1] = op; } void _delete() { int len = strlen(str + 1); int tot = 0; for (int i = 1; i <= len; i++) { if (tot == 0) { tot++; st[tot].num = 1; st[tot].ch = str[i]; } else if (st[tot].ch == str[i]) { st[tot].num++; } else if (st[tot].ch != str[i]) { if (st[tot].num >= 3) { tot--; if (tot != 0 && st[tot].ch == str[i]) { st[tot].num++; } else { tot++; st[tot].ch = str[i]; st[tot].num = 1; } } else { tot++; st[tot].num = 1; st[tot].ch = str[i]; } } } if (tot != 0 && st[tot].num >= 3) tot--; int cnt = 1; for (int i = 1; i <= tot; i++) { for (int j = 0; j < st[i].num; j++) { str[cnt++] = st[i].ch; } } str[cnt] = '\0'; } int main() { char op; cin.getline(str + 1, maxn - 2); scanf("%d", &m); while (m--) { scanf("%d %c", &pos, &op); ins(pos, op); _delete(); if (strlen(str + 1) == 0) { printf("-\n"); } else { printf("%s\n", str + 1); } } return 0; }

文章回顾

大家看了本文祖玛(Zuma)的精彩教程资源内容,是不是对祖玛(Zuma)了解更多,真心希望祖玛(Zuma)能帮助到你, 小编会一直给你带来更多教程资源文章信息。

版权声明:cadd3b662a1f162c 发表于 2020-03-10 11:43:53。

本文由第三方用户分享仅代表作者观点,不代表本网站立场,秉承互联网开放分享的精神,目的在于传递更多信息,加强各行业互通交流,但对内容不作任何保证或承诺,请读者自行参考斟酌。网站发布的信息(包含但不限于版式、图片、字体、文章等素材)由第三方用户分享,版权归原作者所有,本站不承担任何相关的版权纠纷等相关责任。如您认为本篇内容侵犯了您的权益,请与我们联系,我们会及时处理。

豌豆资源网专注分享全网综合资源网站大全,致力于超实用的内容资源搜索。

转载请注明:
本文标题:祖玛(Zuma)
本文地址:https://55wd.com/s4403/

你可能感兴趣

随便逛逛