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/
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 题目分析:
指令操作:
需要支持如下命令:
BACK:把当前页面放入“前进栈”的最顶部,并从“后退栈”中取出顶部元素,使其为当前浏览页面。如果“后退栈”为空,则忽略当前操作。
FORWARD:把当前页面放入“后退栈”的最顶部,并从“前进栈”中取出顶部元素,使其为当前浏览页面。如果“前进栈”为空,则忽略当前操作。
VISIT :将当前页面放入后退栈的顶部,并设置URL为当前指定页面。前进栈设置为空。
QUIT:退出浏览器。
假设浏览器初始登入URL网页:http://www.acm.org/
注意:当你BACK 或者 FORWARD 的时候,如果要转换的页面不存在,需要输出Ignored 的时候,不要把当前的页面加入 前进栈 或者 后退栈。
除非要转换的页面存在,才将当前的页面 加入前进栈或者后退栈。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <iostream>
#include <string>
#include <algorithm> using namespace std; int main()
{
stack<string>s_back; //后退栈
stack<string>s_forw; //前进栈 //假设浏览器初始登入URL网页:http://www.acm.org/
string instruct; //操作指令
string cur="http://www.acm.org/";//当前开始页面
string temp; while(cin>>instruct )
{
if(instruct=="QUIT") break;
if(instruct=="VISIT") //执行此指令后要将当前页面加入后退栈
{ //遇到访问新的页面 //并且前进栈要清空
cin>>temp;
s_back.push(cur); //加入后退栈
cur=temp;
cout<<cur<<endl;
while(!s_forw.empty()) s_forw.pop();
}
if(instruct=="BACK")
{
if(!s_back.empty()){
s_forw.push(cur);//将当前页面加入前进栈 注意:要包含在if语句内 否则会错
cur=s_back.top(); s_back.pop();//不要忘记出栈
cout<<cur<<endl;
}
else printf("Ignored\n");
}
if(instruct=="FORWARD")
{
if(!s_forw.empty()){
s_back.push(cur); // 注意:要包含在if语句内 否则会错
cur=s_forw.top(); s_forw.pop();
cout<<cur<<endl;
}
else printf("Ignored\n");
}
}
return 0;
}
poj 1028 Web Navigation 【模拟题】的更多相关文章
- poj 1028 Web Navigation
Web Navigation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31088 Accepted: 13933 ...
- poj 1028 Web Navigation(模拟)
题目链接:http://poj.org/problem? id=1028 Description Standard web browsers contain features to move back ...
- POJ 1028 Web Navigation 题解
考查代码能力的题目.也能够说是算法水题,呵呵. 推荐新手练习代码能力. 要添加难度就使用纯C实现一下stack,那么就有点难度了,能够使用数组模拟环形栈.做多了,我就直接使用STL了. #includ ...
- poj 1888 Crossword Answers 模拟题
Crossword Answers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 869 Accepted: 405 D ...
- POJ - 1835 宇航员(模拟题)
问题描述: 宇航员在太空中迷失了方向,在他的起始位置现在建立一个虚拟xyz坐标系,称为绝对坐标系,宇航员正面的方向为x轴正方向,头顶方向为z轴正方向,则宇航员的初始状态如下图所示: 现对六个方向分别标 ...
- 1028 Web Navigation
题目链接: http://poj.org/problem?id=1028 题意: 模拟浏览器的前进/后退/访问/退出 的四个操作. 输出当前访问的URL或者Ignore(如果不能前进/后退). 分析: ...
- poj 1208 Web Navigation(堆栈操作)
一.Description Standard web browsers contain features to move backward and forward among the pages re ...
- POJ 2348 Euclid Game (模拟题)
Euclid's Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7942 Accepted: 3227 Des ...
- POJ 1028:Web Navigation
Web Navigation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30828 Accepted: 13821 ...
随机推荐
- 监听EditText字数
editContent.addTextChangedListener(new TextWatcher() { private CharSequence temp;private int editSta ...
- nginx proxy cache配置和清理
1.nginx需要编译Purge模块 2.nginx.conf 配置cache: proxy_cache_path /home/cache/xxx levels=1:2 keys_zone=cac ...
- 自己定义控件三部曲视图篇(二)——FlowLayout自适应容器实现
前言:我最大的梦想,就是有一天.等老了坐在摇椅上回望一生,有故事给孩子们讲--. 相关文章: <Android自己定义控件三部曲文章索引>:http://blog.csdn.net/har ...
- 自己动手开发IOC容器
前两天写简历.写了一句:精通Spring IoC容器.怎么个精通法?还是自己动手写个IOC容器吧. 什么是IoC(Inversion of Control)?什么是DI(Dependency Inje ...
- [译]GLUT教程 - 创建和关闭子窗体
Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Creating and Destroying Subwind ...
- 【数据挖掘】聚类之k-means(转载)
[数据挖掘]聚类之k-means 1.算法简述 分类是指分类器(classifier)根据已标注类别的训练集,通过训练可以对未知类别的样本进行分类.分类被称为监督学习(supervised learn ...
- 三星DRAM+NAND FLASH 合成MCP芯片介绍及应用攻略
转自:http://blog.csdn.net/gao5528/article/details/6256119 三星DRAM+NAND FLASH 合成MCP芯片介绍及应用攻略(K5系列产品篇) 一年 ...
- Foundation框架 - NSDictionary类、NSMutableDictionary类
NSArray.NSSet.NSDictionary /* 集合 1.NSArray\NSMutableArray * 有序 * 高速创建(不可变):@[obj1, obj2, obj3] * 高速訪 ...
- #define的使用方法体会
#define 创建一个宏,该宏是标识符或參数化标识符与标记字符串的关联. 在定义宏之后.编译器可用标记字符串替换源文件里标识符的每一个匹配项. 双击以所有折叠.">语法 #defin ...
- chef简介
Chef 的简单介绍 Chef 主要分为三个部分 Chef Server.Workstation 以及 Chef Client.用户在 Workstation 上编写 Cookbook.然后,通过 k ...