zoukankan      html  css  js  c++  java
  • THU 数据结构 祖玛(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

    这是一道 list 题目,但是清华OJ不能使用 STL,一开始用老是超时,后来借鉴一位大佬用的strcpy, 通过了,觉得非常妙。

    ac代码:

    #include <cmath>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define MAX_N 20005
    
    char ch;
    char s[MAX_N],temp[MAX_N];
    int lens=0,N,pois;
    
    int qui(int a){
        int head=a,tail=a;
        char e=s[a];
    
        while(s[head]==e&&head)    head--;
        if(head||s[head]!=e)    head++;
        while(s[tail]==e&&tail<lens)    tail++;
        
        if(tail-head>=3){
            strcpy(temp,s+tail);
            strcpy(s+head,temp);
            lens=lens+head-tail;
            pois=head;
            return 1;
        }
        else    return 0;
    }
    
    int main(void){    
    
        char c;
        while((ch=getchar())!='
    '){
            s[lens++]=ch;
        }
        s[lens]='';
    
        scanf("%d",&N);
        for(int i=0;i<N;i++){
            scanf("%d %c",&pois,&c);
            strcpy(temp,s+pois);
            strcpy(s+pois+1,temp);
            s[pois]=c;
            lens++;
            while(qui(pois)&&lens);
            if(lens)    puts(s);
            else        puts("-");
        }
    
        return 0;
    }
    

      

  • 相关阅读:
    微服务基础——厉害了!API网关
    11.11 大促背后的秘密——智能合图
    那些我们对2019技术世界趋势的预测都说准了吗?
    DevOps云翼日志服务实践
    技术沙龙|原来落地AI应用是这么回事儿!
    直击JDD | 京东开启技术服务元年:携手合作伙伴,共创产业未来
    直击JDD | 徐雷:智能化零售,以技术为驱动力的突破路径
    直击JDD | 陈生强:京东数科的底层是数字化操作系统
    干货 | Spark Streaming 和 Flink 详细对比
    持续集成与自动化部署
  • 原文地址:https://www.cnblogs.com/jaszzz/p/12599379.html
Copyright © 2011-2022 走看看