Web Navigation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 31906   Accepted: 14242

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

思路:
终于通过这道题目尝试了下C++里面的string类,结果发现尼玛和java里面的完全没两样啊
没有太大的难度,就是按照题目的操作模拟一下就可以,有个地方第一次提交的时候出错了,就是BACK和FORWARD这两种情况
当stack为空的时候,他们是不会把tmp_url压进栈的,这点一开始没有注意到,debug一下就发现了
还有一开始准备用switch,结果发现这个函数只能够对int型变量操作

#include <iostream>
#include <stack>
#include <string>
using namespace std; int main()
{
string command;
stack<string> f,b;
string tmp_url = "http://www.acm.org/";
string new_url; while(cin>>command) {
if(command == "VISIT") {
b.push(tmp_url);
cin>>tmp_url;
while(!f.empty()){f.pop();}
cout<<tmp_url<<endl;
}
if(command == "BACK") {
if(!b.empty()) {
f.push(tmp_url);
tmp_url = b.top();
b.pop();
cout<<tmp_url<<endl;
}
else {
cout<<"Ignored"<<endl;
}
}
if(command == "FORWARD") {
if(!f.empty()) {
b.push(tmp_url);
tmp_url = f.top();
f.pop();
cout<<tmp_url<<endl;
}
else {
cout<<"Ignored"<<endl;
}
}
if(command == "QUIT")
break;
}
return ;
}

POJ-1028(字符串模拟)的更多相关文章

  1. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  2. HDU-3787(字符串模拟)

    Problem Description 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开.现在请计算A+B的结果,并以正常形式输出.   Input 输入包含 ...

  3. HDU-1002.大数相加(字符串模拟)

    本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值. 本题思路:字符串模拟就能过,会Java的大佬应该不会点进来...... 参考代码: #include <cstdio&g ...

  4. Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)

    题目链接:http://codeforces.com/contest/832/problem/B B. Petya and Exam time limit per test 2 seconds mem ...

  5. HDU-Digital Roots(思维+大数字符串模拟)

    The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...

  6. Vigenère密码 2012年NOIP全国联赛提高组(字符串模拟)

    P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简 ...

  7. CCF(JSON查询:40分):字符串+模拟

    JSON查询 201709-3 纯字符串模拟,考的就是耐心和细心.可惜这两样我都缺... #include<iostream> #include<cstdio> #includ ...

  8. poj 1028 Web Navigation(模拟)

    题目链接:http://poj.org/problem? id=1028 Description Standard web browsers contain features to move back ...

  9. poj 1028 Web Navigation 【模拟题】

    题目地址:http://poj.org/problem?id=1028 测试样例: Sample Input VISIT http://acm.ashland.edu/ VISIT http://ac ...

  10. 【stack】模拟网页浏览 poj 1028

    #include<stdio.h> #include<string.h> int main() { ][]; ]; int i,depth; strcpy(s[]," ...

随机推荐

  1. ndroid网络(4):HttpClient必经之路----使用线程安全的单例模式HttpClient,及HttpClient和Application的融合

    上文简 单介绍了HttpClient和Tomcat服务器的交互,主角是HttpClient,然后它跟服务器交互有两种方式即get和post.所以这个 HttpClient就类似于电脑上用的浏览器.当我 ...

  2. ios AFNetworking 有用篇

    在寻常开发中,af是个非常好用的东西.非常喜欢.可是网上的af找了好多都不太全面,不有用.所以我今天做了一个demo.有上传下载的. 比較有用.希望大家可以用到. 去我github下载demo git ...

  3. jquery图片滚动

    注:代码来自17sucai网,已去除部分冗余代码,只保留图片效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...

  4. [转] GDB 下 watch的使用

    这里大概说下gdb调试程序时,watch的使用.至于原理尚不清楚,以后再做补充,还请见谅. watch通常需要和break,run,continue联合使用. 下面举例说明: 代码如下: #inclu ...

  5. NYOJ-571 整数划分(三)

    此题是个非常经典的题目,这个题目包含了整数划分(一)和整数划分(二)的所有情形,而且还增加了其它的情形,主要是用递归或者说是递推式来解,只要找到了递推式剩下的任务就是找边界条件了,我觉得边界也是非常重 ...

  6. Android Studio 简介

    Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具,目前很多开源项目都已经在采用,Google的更新速度也很快,明显能感觉到这是Android开发 ...

  7. MyKTV项目,走起!

    MyKTV项目,走起! 第一部分:这个项目对于新手来说有一点难度,但是当你理清类之间的关系和怎样去实现功能后就会感觉轻松很多. 话不多说,先上类图: 接着是数据库表间关系: 本项目要实现以下功能: 明 ...

  8. activeMQ总结

    队列模式和发布订阅模式的区别 topic只有所有订阅者都消费了,这个消息才会消失.只要有一个订阅者没有消费(持久化模式),这个消息就会存在.订阅者下线然后上线也会读取到这个消息.而且队列的话,消费能力 ...

  9. asp.net微信开发第一篇----开发者接入

    在项目的根目录或者特定的文件夹内,创建一个ashx文件(一般处理程序文件),如图 public void ProcessRequest(HttpContext context) { context.R ...

  10. .net面试总结

    一. hr 为人处事 工作中遇到问题:沟通很重要 离职原因:公司倒闭 二. ISAPI Internet Server Application Program Interface 三. http状态码 ...