POJ1028

 题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1028

 思路:简单的模拟,水题

 

  1. import java.util.Scanner;
  2. import java.util.Stack;
  3. public class Main {
  4.     String command;
  5.     Stack<String> back = new Stack<String>();
  6.     Stack<String> forward = new Stack<String>();
  7.     String current = "http://www.acm.org/";
  8.     String display;
  9.     public Main() {
  10.         Scanner scan = new Scanner(System.in);
  11.         while (!(command = scan.next()).equals("QUIT")) {
  12.             if (command.equals("BACK")) {
  13.                 doBack();
  14.             } else if (command.equals("FORWARD")) {
  15.                 doForward();
  16.             } else if (command.equals("VISIT")) {
  17.                 doVisit(scan);
  18.             }
  19.             System.out.println(display);
  20.         }
  21.     }
  22.     public void doBack() {
  23.         if (back.isEmpty()) {
  24.             display = "Ignored";
  25.         } else {
  26.             forward.push(current);
  27.             current = back.pop();
  28.             display = current;
  29.         }
  30.     }
  31.     public void doForward() {
  32.         if (forward.isEmpty()) {
  33.             display = "Ignored";
  34.         } else {
  35.             back.push(current);
  36.             current = forward.pop();
  37.             display = current;
  38.         }
  39.     }
  40.     public void doVisit(Scanner scan) {
  41.         if (current != null) {
  42.             back.push(current);
  43.         }
  44.         current = scan.next();
  45.         display = current;
  46.         forward.clear();
  47.     }
  48.     public static void main(String[] args) {
  49.         new Main();
  50.     }
  51. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值