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. 如何在 EXCEL 2003 插入的方框内打对勾,复选框

    一个方框里带勾的符号是吧第一种:EXCEL里有个插入符号的功能知道吧,打开它在符号那栏(不是特殊符号那栏),下拉字体找到Wingdings字体,在下面的符号中就能找到框中带勾的符号 第二种:在界面点& ...

  2. Vue.js的小例子--随便写的

    1.领导安排明天给同事们科普下vue 2.简单写了两个小例子 3.话不多说直接上代码 <!DOCTYPE html> <html> <head> <meta ...

  3. Python在线编程环境

    除了安装Python的IDE之外,也可以使用在网页中随时随地编写Python程序. Python官网:https://www.python.org/shell Python123:https://py ...

  4. keil5最新版安装与破解

    1. 下载链接: https://pan.baidu.com/s/1BIrhqmxWdHY7hvihE0Wd4A 密码: cp45 2. 解压缩后得到: 3. 运行mdk526.exe安装keil5. ...

  5. for循环简单实例(打印乘法表,打印菱形)

    关于for循环的简单应用: 回顾了一下for循环的嵌套: for循环嵌套简单来讲就是一个外圈的for程序里面一个套着一个小的for程序,如果在范围内就来回运行计算,超出了就跳出等待 下面程序为打印九九 ...

  6. Caliburn.Micro 杰的入门教程3,事件和参数

    Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...

  7. BZOJ3224_普通平衡树_KEY

    题目传送门 平衡二叉树(Balanced Binary Tree)具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.平衡二叉树的常用实现方法有红 ...

  8. 长沙Uber优步司机奖励政策(12月14日到12月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. eclipse+tomcat配置远程debug调整

    由于开发环境与真实服务器环境存在差异,有时开发时明明正常的逻辑,部署之后就会出现各种各样的问题,通过日志邮不能明确定位到问题的时候,可以采用远程debug调试来定位问题.下面就介绍一下具体的配置步骤: ...

  10. mysql using filesort Using temporary

    using filesort 一般人的回答是: “当行数据太大,导致内存无法容下这些数据产生的临时表时,他们就会被放入磁盘中排序.”  很不幸,这个答案是错的 ,临时表在太大的时候确实会到磁盘离去,但 ...