POJ 1028 Web Navigation

Web Navigation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30689   Accepted: 13750

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/

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
QUIT

Sample 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

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#define N 109
using namespace std;

char a[N];

string s="http://www.acm.org/";

int main()
{
    stack<string>bac,fwd;

    while(~scanf("%s",a))
    {
        if(a[0]=='Q') break;

        if(a[0]=='V')
        {
            bac.push(s);
            cin>>s;
            cout<<s<<endl;
            while(!fwd.empty()) fwd.pop();
        }
        else if(a[0]=='B')
        {
            if(!bac.empty())
            {
                fwd.push(s);
            s=bac.top();
            bac.pop();
            cout<<s<<endl;
            }
            else cout<<"Ignored"<<endl;
        }
        else if(a[0]=='F')
        {
            if(!fwd.empty())
            {
                bac.push(s);
                s=fwd.top();
                fwd.pop();
                cout<<s<<endl;
            }
            else cout<<"Ignored"<<endl;
        }

    }
    return 0;
}
时间: 2024-11-02 01:03:16

POJ 1028 Web Navigation的相关文章

POJ 1028 Web Navigation 题解

考查代码能力的题目.也可以说是算法水题,呵呵. 推荐新手练习代码能力. 要增加难度就使用纯C实现一下stack,那么就有点难度了,可以使用数组模拟环形栈.做多了,我就直接使用STL了. #include <stdio.h> #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<string> forward; s

POJ 1028 Web Navigation (模拟法)

Web Navigation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29819   Accepted: 13328 Description Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features

poj 1028 Web Navigation(模拟题)

题目链接:http://poj.org/problem?id=1028 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 reac

poj 1028 Web Navigation 【模拟题】

题目地址:http://poj.org/problem?id=1028 测试样例: 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 QUIT Sample Output http://acm.ashland.edu/ ht

Web Navigation

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.

zoj 1061 Web Navigation

Web Navigation Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

【POJ 1028】模拟浏览器

本题要求模拟浏览器的前进.后退等操作. 用两个栈实现,一个控制前进,一个控制后退. 在前进与后退操作中,从一个栈中弹出栈顶元素,压入另一个栈中. 当打开一个新网页时,将前进栈清空. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include

poj 1028

他就是个栈,栈就随便写了,贴代码 #include <iostream> #include <stack> #include <string> using namespace std; int main(){ stack<string> stack1,stack2; string str,s1,s2; str="http://www.acm.org/"; stack1.push(str); while(cin>>s1){ if

【POJ 1984】Navigation Nightmare(带权并查集)

Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 100