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. [Angular 2] Dispatching Action with Payloads and type to Reducers

    While action types allow you tell your reducer what action it should take, the payload is the data t ...

  2. 高性能 Socket 组件 HP-Socket v3.2.1-RC5 公布

    HP-Socket 是一套通用的高性能 TCP/UDP Socket 组件,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C+ ...

  3. 常用git命令整理

    花了一点时间来熟悉和整理git常用命令. 推荐的git学习资料:1.搜“Git Community Book 中文版.pdf”,git社区书,内容全面且简明扼要,第一推荐2.搜“Git权威指南.pdf ...

  4. Gamit解算脚本

    这是一个解算单天的shell脚本,对于初学者很有帮助. 首先就是需要在项目(四个字符)建立rinex brdc igs 还有以年纪日命名的目录,然后提前准备好station.info和lfile.文件 ...

  5. C#操作Excel(NPOI)

    这两天需要读取Excel文件,网上找了找,发现NPOI用的是最多的,于是研究了一下.这里大概介绍一下. 首先,在NPOI中一个Excel文件对应了一个IWorkbook对象,Excel中的一个工作表对 ...

  6. MultiWii MWC的软件和调试方法

    (如果你的电脑是win7 64位的系统,安装了JAVA虚拟机后GUI仍然运行不了,那你就需要到C:\Program Files\Java\jre7\bin\找到并复制javaw.exe,然后粘贴到C: ...

  7. python面对对象编程------3:写集合类的三种方法

    写一个集合类的三种方法:wrap,extend,invent 一:包装一个集合类 class Deck: def __init__( self ): self._cards = [card6(r+1, ...

  8. (转)asp.net 使用cookie完成记住密码自动登录

     代码如下 复制代码 string username = this.txtUserName.Text;//用户名        string password = this.txtPassword.T ...

  9. XML and JSON 验证

    function ChkJson(strJson) { //判断Json格式是否正确 if (strJson == null || strJson == "") return tr ...

  10. Android学习手记(4) BroadcastReceiver监听电池信息

    Android 中,Broadcast是一种在应用程序之间进行传输信息的机制.BroadcastReceiver对发送过来的Broadcast进行过滤和响应.根据这种机制,我们可以获取电池现有电量等信 ...