poj 1208 Web Navigation(堆栈操作)
生活随笔
收集整理的這篇文章主要介紹了
poj 1208 Web Navigation(堆栈操作)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、Description Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this.
The following commands need to be supported:
BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored.
FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored.
VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
QUIT: Quit the browser.
Assume that the browser initially loads the web page at the URL http://www.acm.org/
二、題解
??????? 這是挺水的一道題,首先題目就挺簡(jiǎn)單的(英語(yǔ)除外)還告訴我們?cè)趺匆徊讲阶觥R强炊擞⑽念}目完全可以照著做就行了。但是我這道題RE4次,最后發(fā)現(xiàn)是有幾句if判斷省了括號(hào)。以后不能偷懶了,改寫的一定要寫上。
三、題解
import java.util.Scanner; import java.util.Stack;public class Main {public static void main(String[] args) {Scanner sc=new Scanner(System.in);Stack<String> forward=new Stack<String>();Stack<String> backward=new Stack<String>();String current="http://www.acm.org/";String order="";String display="";while(!(order=sc.next()).equals("QUIT")){if(order.equals("VISIT")){backward.push(current);current=sc.next();forward.clear();display=current;}else if(order.equals("BACK")){if(backward.empty()){display="Ignored";}else{forward.push(current);current=backward.pop();display=current;}}else if(order.equals("FORWARD")){if(forward.empty()){display="Ignored";}else{backward.push(current);current=forward.pop();display=current;}}System.out.println(display);}} }
The following commands need to be supported:
BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored.
FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored.
VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
QUIT: Quit the browser.
Assume that the browser initially loads the web page at the URL http://www.acm.org/
Input
Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command.Output
For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command.二、題解
??????? 這是挺水的一道題,首先題目就挺簡(jiǎn)單的(英語(yǔ)除外)還告訴我們?cè)趺匆徊讲阶觥R强炊擞⑽念}目完全可以照著做就行了。但是我這道題RE4次,最后發(fā)現(xiàn)是有幾句if判斷省了括號(hào)。以后不能偷懶了,改寫的一定要寫上。
三、題解
import java.util.Scanner; import java.util.Stack;public class Main {public static void main(String[] args) {Scanner sc=new Scanner(System.in);Stack<String> forward=new Stack<String>();Stack<String> backward=new Stack<String>();String current="http://www.acm.org/";String order="";String display="";while(!(order=sc.next()).equals("QUIT")){if(order.equals("VISIT")){backward.push(current);current=sc.next();forward.clear();display=current;}else if(order.equals("BACK")){if(backward.empty()){display="Ignored";}else{forward.push(current);current=backward.pop();display=current;}}else if(order.equals("FORWARD")){if(forward.empty()){display="Ignored";}else{backward.push(current);current=forward.pop();display=current;}}System.out.println(display);}} }
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
轉(zhuǎn)載于:https://www.cnblogs.com/AndyDai/p/4734158.html
總結(jié)
以上是生活随笔為你收集整理的poj 1208 Web Navigation(堆栈操作)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU-4604 Deque DP
- 下一篇: 性能测试工具比较:LoadRunner