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
Hints
List
描述
祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干个彩色珠子,其中任意三个相邻的珠子不会完全同色。此后,你可以发射珠子到轨道上并加入原有序列中。一旦有三个或更多同色的珠子变成相邻,它们就会立即消失。这类消除现象可能会连锁式发生,其间你将暂时不能发射珠子。
开发商最近准备为玩家写一个游戏过程的回放工具。他们已经在游戏内完成了过程记录的功能,而回放功能的实现则委托你来完成。
游戏过程的记录中,首先是轨道上初始的珠子序列,然后是玩家接下来所做的一系列操作。你的任务是,在各次操作之后及时计算出新的珠子序列。
输入
第一行是一个由大写字母’A’~’Z’组成的字符串,表示轨道上初始的珠子序列,不同的字母表示不同的颜色。
第二行是一个数字n,表示整个回放过程共有n次操作。
接下来的n行依次对应于各次操作。每次操作由一个数字k和一个大写字母Σ描述,以空格分隔。其中,Σ为新珠子的颜色。若插入前共有m颗珠子,则k ∈ [0, m]表示新珠子嵌入之后(尚未发生消除之前)在轨道上的位序。
输出
输出共n行,依次给出各次操作(及可能随即发生的消除现象)之后轨道上的珠子序列。
如果轨道上已没有珠子,则以“-”表示。
样例
见英文题面
限制
0 ≤ n ≤ 10^4
0 ≤ 初始珠子数量 ≤ 10^4
时间:2 sec
内存:256 MB
提示
列表
提示都这么明显了,当然用列表了,不过第一遍写下来有几组数据runtime error了,看了下好像是指针指向非法位置,但最后也没能检查出来。去网上看了下,学习了下大佬,前后加了两个哨兵,数据设为#,果然就避免了非法指针问题。
竟然还有大佬用数组写出来了,好像还很简短,改天学习一下。
代码:
#include
#include
#include
#include
using namespace std;
const int SZ = 1<<20;
struct fastio{
char inbuf[SZ];
char outbuf[SZ];
fastio(){
setvbuf(stdin,inbuf,_IOFBF,SZ);
setvbuf(stdout,outbuf,_IOFBF,SZ);
}
}io; //学到了,好像是增大缓冲流用的,可以缩短程序用时,不太懂。
const int maxn=1e4+10;
char s[maxn];
int n,num=0;
struct bead
{
char color;
bead* up;
bead* next;
}*head,*b1,*b2,*tail;
void build()
{
head=new bead;
tail=new bead;
head->color='#';
head->next = tail;
head->up = NULL;
tail->up = head;
tail->next = NULL;
tail->color = '#';
for(int i = 0;i < num;i++)
{
b1 = new bead;
b1->color = s[i];
if(head->next == tail){b1->up = head;head->next = b1;}
else {b2->next = b1;b1->up = b2;}
b2 = b1;
}
if(num != 0)
{
b2->next = tail;
tail->up = b2;
}
}
void insert_bead(int a,char b)
{
bead* h1 = head->next;
bead* h2 = new bead;
bead* h3;
h2->color = b;
if(a == 0 && head->next == tail)
{
h2->up = head;
h2->next = tail;
head->next = h2;
tail->up = h2;
}
else
{
while(h1->color != '#' && a)
{
h1=h1->next;
a--;
}
h2->up = h1->up;
h2->next = h1;
h1->up->next = h2;
h1->up = h2;
}
int y = 1,y1 = 1;
while(y)
{
int x1 = 1;
h1 = h2->up;
while(h1->color != '#' && h1->color == h2->color)
{
x1++;
h1 = h1->up;
}
h3 = h2->next;
while(h3->color != '#' && h3->color == h2->color)
{
x1++;
h3 = h3->next;
}
if(x1 >= 3)
{
num = num-x1;
if(h1->color == '#' && h3->color == '#')
{
y1 = 0;
head->next = tail;
tail->up = head;
break;
}
h1->next = h3;
h3->up = h1;
if(h1 != head)h2 = h1;
else h2 = h3;
y++;
}
y--;
}
if(y1 == 0)printf("-\n");
else
{
h1 = head->next;
while(h1->color != '#')
{
printf("%c",h1->color);
h1 = h1->next;
}
printf("\n");
}
}
int main()
{
int tt;
char cc,c;
while(~scanf("%c",&c)&&c!='\n')
{
s[num]=c;
num++;
}
build();
scanf("%d",&n);
for(int i = 0;i < n;i++)
{
scanf("%d %c",&tt,&cc);
num++;
insert_bead(tt,cc);
}
return 0;
}