清華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>

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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章