首页 技术 正文
技术 2022年11月10日
0 收藏 833 点赞 3,092 浏览 2161 个字

Stack is a basic data structure. Where 3 operation can be done-

  1. Push: You can push object to the stack

  2. Pop: You can pop the object to the stack

  3. Top: You can check the value of the top object.

For further details you can get idea here ( if you really don’t know ) :https://en.wikibooks.org/wiki/Data_Structures/Stacks_and_Queues

Now we have a problem here, there are N stack in front of you. they are numbered from 1 to N. Each of them are initially empty. Now you will have Q operations. Each operation can be one the below 4 types:

  1. push i x, Push a value of x to stack numbered i

  2. pop i, Pop value from the stack numbered i, if stack is empty discard the operation

  3. put i j, Put the j’th stack on top of the i’th stack. So there will be no element left on the j’th stack.

  4. top i, Print the value of the top element of ith stack. If stack is empty print “Empty!”

Check the Sample IO for further understanding…

Input:

Input starts with an integer T (≤5), denoting the number of test cases.

The first line of a case is a blank line. The next line contains two integers N (1 ≤ N ≤ 104), Q(1 ≤ Q ≤ 5*104).

The next Q lines will contain a operation like above mentioned.

(1≤ I, j ≤ N), (1≤ x ≤ 105)

Output

For each test case, print the case number in a single line. Then for each 4th type operation you should print the value or “Empty!” if the stack is empty.

Input

Output

1

3 18

push 1 1

push 2 2

push 3 3

push 3 4

top 1

top 2

top 3

put 1 3

pop 2

top 1

top 2

top 3

pop 1

top 1

pop 1

top 1

pop 1

top 1

Case 1:

1

2

4

4

Empty!

Empty!

3

1

Empty!

Judge data is huge so use faster IO like scanf/printf

题意:模拟栈的操作。

思路:本来是水题一道,但是主要很细节,所以还是放这里,方便提醒自己注意细节。

push元素的时候更新栈首begin和栈定Laxt。

pop的时候如果空了,注意更新Begin=0;

put x到y的时候如果x为空或者x==y的时候不不操作。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int Laxt[maxn],Begin[maxn],fa[maxn],cnt,num[maxn];
char c[];
int main()
{
int T,N,Q,Case=,x,i,j;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&Q);
printf("Case %d:\n",++Case);
memset(Laxt,,sizeof(Laxt));
memset(Begin,,sizeof(Begin));
memset(fa,,sizeof(fa)); cnt=;
while(Q--){
scanf("%s",c);
if(c[]=='s'){ //push
scanf("%d%d",&i,&x);
num[++cnt]=x;
if(!Begin[i]) Begin[i]=cnt;
fa[cnt]=Laxt[i];
Laxt[i]=cnt;
}
else if(c[]=='t'){ //put
scanf("%d%d",&i,&j);
if(i!=j&&Laxt[j]){
fa[Begin[j]]=Laxt[i];
Laxt[i]=Laxt[j];
if(!Begin[i]) Begin[i]=Begin[j];
Laxt[j]=Begin[j]=;
}
}
else if(c[]=='p'){ // pop
scanf("%d",&i);
if(Laxt[i]) Laxt[i]=fa[Laxt[i]];
if(!Laxt[i]) Begin[i]=;
}
else { //top
scanf("%d",&i);
if(!Laxt[i]) printf("Empty!\n");
else printf("%d\n",num[Laxt[i]]);
}
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291