【POJ - 1028】 Web Navigation( 栈 or 模拟队列 )
題干:
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/
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.
Sample Input
VISIT http://acm.ashland.edu/ VISIT http://acm.baylor.edu/acmicpc/ BACK BACK BACK FORWARD VISIT http://www.ibm.com/ BACK BACK FORWARD FORWARD FORWARD QUITSample Output
http://acm.ashland.edu/ http://acm.baylor.edu/acmicpc/ http://acm.ashland.edu/ http://www.acm.org/ Ignored http://acm.ashland.edu/ http://www.ibm.com/ http://acm.ashland.edu/ http://www.acm.org/ http://acm.ashland.edu/ http://www.ibm.com/ Ignored?
來自一個博客的分析如下:http://www.mamicode.com/info-detail-428744.html
題意簡述:這題是一個模擬WEB瀏覽器的程序,默認主頁為http://www.acm.org/輸入為指令,出書為當前頁面的網址。 輸入的指令有4種:(1)VISIT(后接一個網址)——訪問它后面緊接著的網址。(2)BACK——訪問當前網頁的前一個。(3)FORWARD——訪問當前網頁的后一個。(4)QUIT——退出(關閉瀏覽器)。思路:必須記錄訪問過的網址,因為是順序關系,所以用數組存儲,下標為0的元素為默認頁;有一個變量記錄當前頁面在數組中的位置;BACK和FORWARD指令可能出現特殊情況:(1)BACK到默認頁面再BACK——輸出Ignored。(2)FORWARD到最后一個訪問到的頁面再與FORWARD——輸出Ignored。這樣,就有一個問題:要記錄最后一個訪問到的頁面,對于這個問題只需要在遇到VISIT指令的時候順便操作上面的指令的對應操作就變得很簡單。VISIT——就將網址寫入數組后再進行輸出,記錄當前位置,這個位置也即最后一個訪問到的頁面。BACK——當前頁面減1,輸出網址,注意判斷特殊情況。FORWARD——當前頁面+1,輸出網址,也要注意特殊情況。QUIT——退出。?
AC代碼:
#include<cstdio> #include<cstring> #include<iostream>using namespace std; //stack <string> sk; char s[1000][100]; int main() { // freopen("in.txt","r",stdin);char op[10];int top = 0,maxx = 0;strcpy(s[0],"http://www.acm.org/");while(scanf("%s",op) ) {if(!strcmp(op,"QUIT")) break;else if(op[0] == 'V') {scanf("%s",s[++top]);maxx = top;printf("%s\n",s[top]);}else if(op[0] == 'B') {if(top >= 1) {top--;printf("%s\n",s[top]);}else if(top == 0) {printf("Ignored\n");} }else if(op[0] == 'F') {top++;if(top>maxx) {printf("Ignored\n");top--;}else printf("%s\n",s[top]);}}return 0 ; }?
AC代碼2:(棧)
#include <stdio.h> #include <string.h> const int maxn = 75, N = 10005; char q[N][maxn], queue[N][maxn], str[maxn]; char ch[maxn]; int main ( ) {int T, top, front, cas = 0;//freopen ( "in0.in", "r", stdin );scanf ( "%d", &T );while ( T -- ){front = top = -1;strcpy ( q[++ top], "http://www.acm.org/" );//開始就將一個網址加入棧if ( cas ++ ) //每組數據有換行printf ( "\n" );while ( ~ scanf ( "%s", str ) && strcmp ( str, "QUIT" ) != 0 ){if ( strcmp ( str, "VISIT" ) == 0 ){ //查看時將所有可以后退的全部去掉scanf ( "%s", ch );printf ( "%s\n", ch );strcpy ( q[++ top], ch );front = -1;}else if ( strcmp ( str, "BACK" ) == 0 ){if ( top <= 0 ) //小于等于0證明不能在后退了printf ( "Ignored\n" );else{//將此網頁加入可以前進的棧中strcpy ( queue[++ front], q[top] );printf ( "%s\n", q[-- top] );}}else{if ( front < 0 )printf ( "Ignored\n" );else{//將前進的網頁加到后退中strcpy ( q[++ top], queue[front] );printf ( "%s\n", queue[front --] );}}}}return 0; }?
總結
以上是生活随笔為你收集整理的【POJ - 1028】 Web Navigation( 栈 or 模拟队列 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新信用卡额度是多少 额度高低由申卡人财力
- 下一篇: 新信用卡申请临时额度 过了有效期不还罚金