清华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
这里是 - (和CSDN输入冲突所以加上了描述)

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.首先输入的时候要注意使用cin.getline(),因为初始珠子数量可能为0,所以可能读入"/n"
注意:使用fgets()函数会错误
2.定义数组时要开双倍大小20000,否则a[i]=a[i+sub](sub可能等于10000)会越界
3.然后写一个函数按题目的要求模拟即可。从头对字符串扫描,插入字符,消除字符
(若成功消除,则从头扫描),返回操作后的字符串长度,作为是否输出’-'的依据

注意可能不止三个连在一起

我的代码:

#include <iostream>
#include <cstring>
#include <cstdio>
//如果是中国石油大学的同学,请不要抄袭,不然0分处理时很尴尬,请独立完成,有不理解的地方可与我交流
using namespace std;
char s[20030];
int solve(int pos,char c, int len)
{
    for(int i=len; i>pos; i--)
        s[i] = s[i-1];
    s[pos] = c;
    len++;
    for(int i=0; i<len-2; i++)
    {
        if(s[i]==s[i+1] && s[i+1]==s[i+2])
        {
            int r,sub;
            for(r = i+2; r<len && s[r]==s[i]; r++);
            sub = r-i;
            for(int j = i; j<len; j++)
            {
                s[j] = s[j+sub];
            }
            len-=sub;
            i = -1;  //i=-1返回后i++就又从零开始了
        }
    }
    return len;
}


int main()
{
    cin.getline(s,10005);
    int len = strlen(s);
    int m; cin>>m;
    while(m--){
        int pos;
        char c;
        scanf("%d %c",&pos, &c);
        len = solve(pos,c,len);
        if(len)
            printf("%s\n",s);
        else
            printf("-\n");
    }
    return 0;
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值