import java.util.*; public class LANQIAO1 { // 装入当前及之前网站 public static Stack<String> stack = new Stack<>(); // 装入被弹出的网站,以便于前进访问 public static Stack<String> stack2 = new Stack<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String op; String web; for (int i = 0; i < n; i++) { op = sc.next(); // 访问情况,栈2不空则弹出(因为无法前进了),将当前网站压入栈1 if(op.equals("VISIT")){ web = sc.next(); while (!stack2.empty()){ stack2.pop(); } stack.push(web); // 后退情况,栈1要始终保持1个基网站,如果栈1的网站数大于1,则弹出一个,让下面的网址成为栈顶,等到循环体最后输出,并将其压入栈2 }else if(op.equals("BACK")){ if(stack.size() <= 1) { System.out.println("Ignore"); continue; } stack2.push(stack.peek()); stack.pop(); // 前进情况,先看2栈是否有值,如果没有则无法前进,有则将栈2栈顶元素放入栈1栈顶 }else{ if(stack2.empty()){ System.out.println("Ignore"); continue; } stack.push(stack2.peek()); stack2.pop(); } System.out.println(stack.peek()); } } }
蓝桥杯真题-网站访问
最新推荐文章于 2024-03-14 21:18:38 发布