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. (转) [教程] Unity3D中角色的动画脚本的编写(一)

    ps: 这两天研究unity3d,对动画处理特别迷糊,不知FBX导入以后,接下来应该怎么操作,看到这篇文章,感觉非常好,讲解的很详细. 已有好些天没写什么了,今天想起来该写点东西了.这次我所介绍的内容 ...

  2. 简单题思维转化BestCoder

    题意:给你a, b, c, d四个数,这几个数的范围都是大于0小于1000的整数,让比较 a ^b 和 c ^ d的大小. 这道题看着特别简单,但是当时就是做不出来,将近一个月没有做题了,手生了,不过 ...

  3. jQuery自定义组件——输入框设置默认提示文字

    if (window.jQuery || window.Zepto) { /** * 设置输入框提示信息 * @param options * @author rubekid */ var setIn ...

  4. html跳转到同一个页面的不同位置

    <html> <body> <p><a href="#C4">查看 Chapter 4.</a></p> & ...

  5. Namespace declaration statement has to be the very first

    Namespace declaration statement has to be the very first statement in the script 我新建了一个Homea模块,并把Hom ...

  6. Windows8 正式版最简单的去除桌面水印方法

    方法一: 优点:无需替换文件,无需任何工具,对系统没有副作用缺点:更换主题或者壁纸之后水印再现方法:按住 “win键+P” 进入 “第二屏幕 ”选择 “扩展”再按住 “win键+P” 进入 “第二屏幕 ...

  7. UITableViewCell实现3D缩放动画

    gif效果图: 代码部分: import UIKit class TableViewController: UITableViewController { override func viewDidL ...

  8. 去掉eclipse上编辑时的提示

    用eclipse时,鼠标移到类上时会给出提示,如下图:

  9. sqoop 1.4.4-cdh5.1.2快速入门

    一.快速入门 (一)下载安装 1.下载并解压 wget http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.4-cdh5.1.2.tar.gz tar - ...

  10. C++ Primer 5th 第7章 类

    类的基本思想是数据抽象和封装,定义类就是定义一个抽象数据类型. 类中的所有成员必须在类中声明,也即默认定义在类中的成员全部为声明,除非显式的定义成员函数的函数体.成员函数是在类中声明的,定义可以在类内 ...