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. You are asked to implement this. The commands are:

  1. BACK: If the backward stack is empty, the command is ignored. Otherwise, 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.
  2. FORWARD: If the forward stack is empty, the command is ignored. Otherwise, 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.
  3. VISIT <url>: 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.
  4. QUIT: Quit the browser.

The browser initially loads the web page at the URL 'http://www.lightoj.com/'

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains some commands. The keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 50 characters. The end of case is indicated by the QUIT command and it shouldn't be processed. Each case contains at most 100 lines.

Output

For each case, print the case number first. For each command, print the URL of the current page (in a line) after the command is executed if the command is not ignored. Otherwise, print 'Ignored'.

Sample Input

1

VISIT http://uva.onlinejudge.org/

VISIT http://topcoder.com/

BACK

BACK

BACK

FORWARD

VISIT http://acm.sgu.ru/

BACK

BACK

FORWARD

FORWARD

FORWARD

QUIT

Sample Output

Case 1:

http://uva.onlinejudge.org/

http://topcoder.com/

http://uva.onlinejudge.org/

http://www.lightoj.com/

Ignored

http://uva.onlinejudge.org/

http://acm.sgu.ru/

http://uva.onlinejudge.org/

http://www.lightoj.com/

http://uva.onlinejudge.org/

http://acm.sgu.ru/

Ignored

题目意思:利用栈模拟一下浏览器访问网页的过程。

解题思路:建立两个栈,分别储存向前和向后的元素,其实就是在来回倒。

唉,好久没做题了,碰上这一道栈的题,来回做了好久,不过也是在补之前的漏洞,之前就碰到过这样一道双栈的题目,然而并没有及时补题,拖到了现在。

 #include<iostream>
#include<stack>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main()
{
int t,count=;
string x,y;
scanf("%d",&t);
while(t--)
{
printf("Case %d:\n",count++);
stack<string>s1;
stack<string>s2;
s1.push("http://www.lightoj.com/");
while()
{
cin>>x;
if(x[]=='Q')
{
break;
}
else if(x[]=='V')
{
cin>>y;
s1.push(y);
cout<<y<<endl;
while(!s2.empty())///清空
{
s2.pop();
}
}
else if(x[]=='B')///因为s1栈顶元素是当前访问的页面,后退一步必须返回当前栈顶的下一个元素。
{
if(s1.size()>)///此时栈内必须有两个以上的元素
{
s2.push(s1.top());
s1.pop();///删掉当前的页面
cout<<s1.top()<<endl;
}
else
{
cout<<"Ignored"<<endl;
}
}
else if(x[]=='F')
{
if(!s2.empty())
{
s1.push(s2.top());
cout<<s2.top()<<endl;
s2.pop();///删掉当前的页面
}
else
{
cout<<"Ignored"<<endl;
}
}
}
}
return ;
}

Discover the Web(栈模拟)的更多相关文章

  1. web前端面试系列 - 数据结构(两个栈模拟一个队列)

    一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将 ...

  2. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  3. UVALive 3486/zoj 2615 Cells(栈模拟dfs)

    这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...

  4. UVALive 7454 Parentheses (栈+模拟)

    Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...

  5. poj1363Rails(栈模拟)

    主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...

  6. 【LintCode·容易】用栈模拟汉诺塔问题

    用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...

  7. 51Nod 1289 大鱼吃小鱼 栈模拟 思路

    1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...

  8. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

  9. HDOJ 4699 Editor 栈 模拟

    用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

随机推荐

  1. tomcat启动startup.bat一闪而过【亲测有效】

    遇到很多次运行startup.bat后,一个窗口一闪而过的问题,但是从来没去纠正怎样修改配置才是正确的,现在从网上查阅的资料整理如下:tomcat在启动时,会读取环境变量的信息,需要一个CATALIN ...

  2. ABAP术语-SAP GUI for HTML

    SAP GUI for HTML 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/14/1104996.html An ITS impleme ...

  3. Linux下安装 nginx

    安装依赖 yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install openssl openssl- ...

  4. DOCTYPE导致MyEclipse无法正常格式化HTML的问题

    今天遇到在JSP代码中Ctrl+F无法正常格式化HTML代码,经过排查是DOCTYPE的原因. 之前写的是: <!DOCTYPE html PUBLIC "-//W3C//DTD XH ...

  5. hql返回数值

    public int getCountUser() throws ParseException { Session hSession = sessionFactory.getCurrentSessio ...

  6. ASP.NET Core学习网站推荐

    跟大家推荐一个不错的学习.NET Core 的网站,这个网站的视频是付费的,但是录视频的都是.NET Core的大佬们,个人觉得很不错推荐出来 video.jessetalk.cn

  7. try catch finally 中 returne的执行顺序

    结论:1.不管有没有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  8. Linux的数据传输

    1. sz 与 rz sz:将选定的文件从本地发送(send)到远端机器 rz:运行该命令会弹出一个文件选择窗口,从本地选择文件夹,接收(receive)从远端的文件 mac 下使用 brew 安装: ...

  9. 树莓派3B+学习笔记:3、启用root账户

    1.打开终端,输入 sudo passwd root 输入两次密码后设置root账户密码: 2.输入 sudo passwd --unlock root 解锁root账户: 3.点击主菜单的“Shut ...

  10. 20190118-自定义实现replace方法

    1.自定义实现replace方法 Python replace() 方法把字符串中的 old(旧字符串) 替换成 neange(新字符串),如果指定第三个参数max,则替换不超过 max 次.考虑ol ...