Java实现:
import java.util.Scanner;
import java.util.Stack;public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Stack<String> s1=new Stack<String>();
Stack<String> s2=new Stack<String>();
int n=in.nextInt();
for(int i=0;i<n;i++){
String s11;
String s12;
s11=in.next();
String temp;
if(s11.equals("VISIT")){
s12=in.next();
while(!s2.empty()){
s2.pop();
}
s1.push(s12);
System.out.println(s1.peek());
}else if (s11.equals("BACK")){
if(s1.empty()){
System.out.println("Ignore");
}else{
s2.push(s1.peek());
s1.pop();
if(!s1.empty()){
System.out.println(s1.peek());
}else{
System.out.println("Ignore");
s1.push(s2.peek());
s2.pop();
}
}
}else {
if(s2.empty()){
System.out.println("Ignore");
}else{
if(!s2.empty()){
System.out.println(s2.peek());
s1.push(s2.peek());
s2.pop();
}else{
System.out.println("Ignore");
}
}
}
}
}
}