祖瑪(Zuma)


清華OJ——數據結構與算法實驗(中國石油大學)

祖瑪(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.

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

 

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #define N 100500
 5 using namespace std;
 6 
 7 char s[N];
 8 int len;
 9 
10 void print()
11 {
12     if(!len)
13     {
14         printf("-\n");
15         return;
16     }
17 
18     for(int i=0;i<len;i++)
19     printf("%c",s[i]);
20     printf("\n");
21 
22 }
23 
24 void shoot(int pos,char k)
25 {
26     for(int i=len;i>pos;i--)s[i]=s[i-1];
27     s[pos]=k;
28     len++;
29 
30     //print();
31     int l=pos,r=pos+1,sum=0;
32     int ansl=l,ansr=r;
33 
34     while(l>=0&&s[l]==s[pos])l--,sum++;
35     while(r<len&&s[r]==s[pos])r++,sum++;
36     while(sum>=3)
37     {
38         ansl=l;
39         ansr=r;
40 
41         pos=l;
42         sum=0;
43 
44         while(l>=0&&s[l]==s[pos])l--,sum++;
45         while(r<len&&s[r]==s[pos])r++,sum++;
46     }
47     l=ansl;
48     r=ansr;
49 
50    // printf("%d %d\n",l,r);
51     int cut=r-l-1;
52     for(int i=l+1;i<=l+len-r;i++)s[i]=s[r+i-l-1];
53     len-=cut;
54     print();
55 
56 }
57 
58 
59 int main()
60 {
61     int t;
62     cin.getline(s,sizeof(s));
63     scanf("%d",&t);
64     //scanf("%s %d",s,&t);
65     len=strlen(s);
66 
67     while(t--)
68     {
69         char ss[10];
70         int pos;
71         scanf("%d %s",&pos,ss);
72         shoot(pos,ss[0]);
73     }
74     return 0;
75 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM