Discover the Web(栈模拟)
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:
- 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.
- 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.
- 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.
- 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(栈模拟)的更多相关文章
- web前端面试系列 - 数据结构(两个栈模拟一个队列)
一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将 ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- UVALive 7454 Parentheses (栈+模拟)
Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...
- poj1363Rails(栈模拟)
主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...
- 【LintCode·容易】用栈模拟汉诺塔问题
用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...
- 51Nod 1289 大鱼吃小鱼 栈模拟 思路
1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...
- Code POJ - 1780(栈模拟dfs)
题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...
- HDOJ 4699 Editor 栈 模拟
用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
随机推荐
- laravel 5.7 resources 本地化 简体中文
使用方法: 新建目录[项目目录/resources/lang/zh] 按以下内容创建文件,并将内容复制到文件中 修改 config/app.php 'locale' => 'zh', 'fall ...
- ubuntu包管理机制
1 ubuntu包管理机制 跟大家分享一下ubuntu的软件管理机制.如果你们有过: apt-get install 或者 apt-get update 失败的经历. 在众多的apt命令中迷失. 疑惑 ...
- MySQL 开启事件 使用定时器调用存储过程
mysql定时器是系统给提供了event,而oracle里面的定时器是系统给提供的job.废话少说,下面创建表:create table mytable (id int auto_incremen ...
- 移动端网站通用模板 单位rem
html <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...
- visual studio进程或线程自上一个步骤以来已更改
1.自己的解决方案:visual studio在多进程执行,在配置页面(webconfig)里把UseCounterThread参数设置为0 2.公司其他人解决方案,自己试了,多进程执行的时候没起作用 ...
- Active Job 基础
开发中涉及到调用三方服务API,运行时间长,结果不需要实时反馈给用户这样的任务,都可以使用异步处理.常见的场景包括:发邮件和短信.图片处理.定时清理等.爬虫. 后端处理软件可以自行选择这里选择了sid ...
- 为 ItemsControl 类型的控件提供行号,mvvm模式 绑定集合
从网络上看到的两种方式,一种是,在 codebehind 里为 控件写事件,下面是将集合绑定到 DataGrid 控件: private void DataGridSoftware_LoadingRo ...
- Python破解压缩包密码问题
所用知识 1. Pool 进程池 2. try...except 异常处理 3.枚举的方式 4.生成器的运用 逻辑关系 通过生成假密码去碰撞!捕获异常,一直碰撞,直到生成的密码与压缩包建立的密码对应, ...
- dvs-panotracking编译运行
编译运行dvs-panotracking > 编译dvs-panotracking之前首先需要安装imageutilities . 源码下载 https://github.com/VLOGrou ...
- vue中开发webSocket
先安装 sockjs-client 和 stompjs npm install sockjs-client npm install stompjs <template> <div&g ...