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. txt文件导入mysql--转

    MySQL写入数据通常用insert语句,如 insert into person values(张三,20),(李四,21),(王五,70)…; 但有时为了更快速地插入大批量数据或交换数据,需要从文 ...

  2. 从零开始写一个Tomcat(贰)--建立动态服务器

    上文书说道如何通过http协议建立一个静态的服务器来访问静态网页,但我们选择tomcat最主要的原因还是因为它能动态的执行servlet,这边文章将引导你实现一个能够运行servlet的服务器,这个简 ...

  3. Sql 语句添加字段、修改字段类型、默认值语法

    Sql 语句添加字段 ,) not null --修改类型 alter Table bbs ) Sql 语句修改默认值 alter table 表名 drop constraint 约束名字 --删除 ...

  4. Creating a Swap Partition

    Creating a Swap Partition 1.   Use  fdisk /dev/vda  to open your disk in fdisk. (Use gdisk if you ar ...

  5. Freemarker常用技巧(二)

    1 list.break指令<#list sequence as item>  ...</#list>tem_index:当前变量的索引值.item_has_next:是否存在 ...

  6. javascript:运动框架

    function startMove(obj,json,fnEnd) { clearInterval(obj.timer);//清除定时器 obj.timer=setInterval(function ...

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

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

  8. spring aop原理分析

    持续更新... aop跟java代理模式有关. java.lang.reflect.Proxy java.lang.reflect.InvocationHandler 工厂模式用到java反射. ao ...

  9. RTX管理器服务运行状态空白

    A)打开RTX管理器安装目录下的bin文件夹,运行convert.bat 右键计算机——管理——服务——找到以RTX开头的服务,按RTX_ConfigCenter.RTX_HTTPServer. RT ...

  10. PHP 类型比较表

    以下的表格显示了 PHP 类型和比较运算符在松散和严格比较时的作用.该补充材料还和类型戏法的相关章节内容有关.同时,大量的用户注释和 » BlueShoes 的工作也给该材料提供了帮助. 在使用这些表 ...