URAL 1992

CVS

Description

Yoda: Visit I will the cloners on Kamino... And see this army they have created for the Republic.

Cloners from the Kamino planet breed some of the finest clones. Such good results are due to the careful management over the clones’ evolution. The Kaminuans are now busy working out a new study technology that lets increase the clones’ effectiveness. The cloners have come up with a new control system CVS (Clone Version System) that makes managing the progress of experiments easier. The system is quite simple to use.

The Kaminuans have some set of educational programs at their disposal. A clone’s effectiveness depends on which programs and in which order he has learned. The Kaminuans can teach any clone a program as long as this clone hasn’t learned it already.

To make the experiments even easier to conduct, the Kaminuans enabled canceling the changes made by the last program the clone has learned. In this case, the clone’s knowledge returns to the level when the program hasn’t yet been studied. Then this clone can study this program in the future. You can cancel programs at any time unless the clone is at the basic knowledge level.

Besides the ‘roll back’ function, a clone can ‘re-learn’ a program. If one cancels some program by mistake, he can cancel the cancellation. The CVS keeps record of each clone’s canceled programs. After a program is canceled, the CVS adds another record. After a clone re-learn a program, the record is deleted. If a clone learn (not relearn) a program, all cancellation record history of this clone is deleted. You can use the re-learn function as long as the record history for this clone contains any records.

Finally, the system has a ‘clone’ option. If a Kaminuan likes the current variant of a clone, he can clone the clone, that is, create a new clone with the same sequence of taught programs and cancellation history.

Initially the Kaminuans have a single clone with basic knowledge. Help them analyze the progress of the experiments.

Input

The first line of the input contains numbers n — the number of queries — and m — the number of educational programs (1 ≤ nm ≤ 5·10 5). Each of the following n lines has one of the formats given below.

  • learn ci pi. Teach clone ci program pi (1 ≤ pi ≤ m).
  • rollback ci. Cancel the last learned program for clone ci.
  • relearn ci. Apply ‘re-learn’ function to clone ci.
  • clone ci. Clone the clone ci.
  • check ci. Display the last program clone ci has learned and knows at the moment.

It is guaranteed that rollback won’t be applied to the clone that is at the basic knowledge level. learn is always applied with the program a clone doesn’t already know. relearn is only applied if the cancellation history of a clone is not empty. It is also guaranteed that only the clones that already exist can occur in the queries. The numbers are assigned to the clones in the order the clones appear. The Kaminuans started their experiments from clone number one.

Output

For each check  ci query display the result on a single line. If some clone has only basic knowledge, print basic, otherwise print the number of the last learned program.

Sample Input

input output
9 10
learn 1 5
learn 1 7
rollback 1
check 1
clone 1
relearn 2
check 2
rollback 1
check 1
5
7
basic

题目值得一做

解析:

通过两个线性表来实现前进后退操作。

#include <cstdio>
#include <iostream>
#define N 500005
using namespace std;
int now[N],tot_ro,c,p;
int stack[N],pre[N],tot;
int stack_ne[N],now_ne[N],next[N],tot_ne;
char order[100];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        tot=tot_ne=0;
        tot_ro=1;
        now[1]=now_ne[1]=0;
        for(int i=0; i<n; i++){
            scanf("%s",order);
            switch (order[4]) {
                case ‘n‘://learn
                    scanf("%d%d",&c,&p);
                    stack[++tot]=p;
                    pre[tot]=now[c];
                    now[c]=tot;
                    now_ne[c]=0;
                    break;
                case ‘b‘://rollback
                    scanf("%d",&c);
                    if(now[c]!=0){
                        stack_ne[++tot_ne]=stack[now[c]];
                        next[tot_ne]=now_ne[c];
                        now_ne[c]=tot_ne;
                        now[c]=pre[now[c]];
                    }
                    break;
                case ‘a‘://relearn
                    scanf("%d",&c);
                    if(now_ne[c]!=0){
                        stack[++tot]=stack_ne[now_ne[c]];
                        pre[tot]=now[c];
                        now[c]=tot;
                        now_ne[c]=next[now_ne[c]];
                    }
                    break;
                case ‘e‘://clone
                    scanf("%d",&c);
                    if(c<=tot_ro){
                        now[++tot_ro]=now[c];
                        now_ne[tot_ro]=now_ne[c];
                    }
                    break;
                case ‘k‘://check
                    scanf("%d",&c);
                    if(now[c]==0)
                        printf("basic\n");
                    else
                        printf("%d\n",stack[now[c]]);
                    break;
            }
        }
    }
    return 0;
}

URAL 1992,布布扣,bubuko.com

时间: 2024-08-06 11:54:53

URAL 1992的相关文章

灵活利用单链表,顺带一提可持久化链表。

#include<stdio.h> /** 单链表 **/ /* 利用指针可以为直接映射到改变上 且后续的地址传递比较方便,永远不要对表头做操作.只赋值. */ struct Node { int val; Node * next; }; Node * Head,Last; void CreatList_L(int n) { Node *p,*q; Head = new Node(); Head->next = NULL; p = Head; while(n--) { q = new N

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

ural 1219. Symbolic Sequence

1219. Symbolic Sequence Time limit: 1.0 secondMemory limit: 64 MB Your program is to output a sequence of 1 000 000 lowercase Latin letters. This sequence should satisfy the following restrictions: Every letter occurs not more than 40 000 times in th