题目链接:http://poj.org/problem?id=1028
我的相同博文参考:https://blog.csdn.net/qq_21201267/article/details/88938360
LeetCode 5430. 设计浏览器历史记录(双栈)
解题思路参考上面博文。直接贴出代码:
#include <stack>
#include <iostream>
#include <string>
using namespace std;
int main()
{
stack<string> webstackmain, webstacktemp;
webstackmain.push("http://www.acm.org/");
string function,web;
while(cin >> function && function != "QUIT")
{
if(function == "VISIT")
{
cin >> web;
webstackmain.push(web);
for(int i = webstacktemp.size(); i != 0; i--)
{
webstacktemp.pop();
}
cout << webstackmain.top() << endl;
}
else if(function == "BACK")
{
if(webstackmain.size() == 1)
cout << "Ignored" << endl;
else
{
web = webstackmain.top();
webstackmain.pop();
webstacktemp.push(web);
cout << webstackmain.top() << endl;
}
}
else //function == "FORWARD"
{
if(webstacktemp.empty())
cout << "Ignored" << endl;
else
{
web = webstacktemp.top();
webstacktemp.pop();
webstackmain.push(web);
cout << webstackmain.top() << endl;
}
}
}
return 0;
}