链接 POJ 1028
题意:模拟一个网页浏览的过程,给出起始页面,有三种命令:
①visit url 即访问某URL,当前页面变为该页。并且清空此页面的所有“前驱”
②back 后退,倒退到前一个网页
③forward 前进,同理如上
④quit 退出,结束整个命令输入,这一行输入不用做任何处理,结束程序即可
很明显是模拟一个先进先出的结构,故用stack,考虑到进行后退操作时,浏览过的页面可能在接下来被再次访问到(即执行forward命令),故再开另一个栈来存储它们
另外,URL的存储问题,最开始写的时候用纯C语言来写,用字符串数组存储URL,这个读入就特别麻烦。。。而且visit命令中visit和要访问的URL是在同一行内的,识别起来很费劲。后来实在受不了了直接用c++里面的string类,问题迎刃而解,代码变得非常简洁。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<string>
#include<cmath>
#include<stack>
//#define file
#define maxn 100010
using namespace std;
string s;
char order[15];
stack<string>s1,s2;
int main()
{
#ifdef file
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // file
s1.push("http://www.acm.org/");
while(cin>>order)
{
if(order[0]=='Q')
break;
else if(order[0]=='V')
{
cin>>s;
s1.push(s);
string tmp=s1.top();
cout<<tmp<<endl;
while(!s2.empty())s2.pop();
}
else if(order[0]=='B')
{
s=s1.top();
s1.pop();
if(!s1.empty())
{
s2.push(s);
cout<<s1.top()<<endl;
}
else
{
s1.push(s);
cout<<"Ignored"<<endl;
}
}
else
{
if(!s2.empty())
{
s=s2.top();
s2.pop();
s1.push(s);
cout<<s1.top()<<endl;
}
else
{
cout<<"Ignored"<<endl;
}
}
}
}