网页跳转
题目描述
小明每天都在用一款浏览器软件。在这个浏览器中,一共三种操作:打开页面、 回退和前进。它们的功能如下:
- 打开页面:在地址栏中输入网址,并跳转到网址对应的页面;
- 回退:返回到上一次访问的页面;
- 前进:返回到上次回退前的页面,如果上一次操作是打开页面,那么将无法前进。
现在,小明打开浏览器,进行了一系列操作,你需要输出他每次操作后所在页面的网址。
输入描述
第一行输入一个整数 n,表示小明的操作次数。接下来一共 n 行,每行首先输入一个字符串,如果是VISIT,后面接着输入一个不含有空格和换行的网址,表示小明在浏览器地址栏中输入的网址;如果是BACK,表示小明点击了回退按钮;如果是 FORWARD,表示小明点击了前进按钮。
输出描述
对于每次操作,如果小明能操作成功,输出小明操作之后的网址,否则输出 Ignore。假设小明输入的所有网址都是合法的。
用例输入 #1
10
VISIT https://www.jisuanke.com/course/476
VISIT https://www.taobao.com/
BACK
BACK
FORWARD
FORWARD
BACK
VISIT https://www.jisuanke.com/course/429 FORWARD
BACK
用例输出 #1
https://www.jisuanke.com/course/476
https://www.taobao.com/
https://www.jisuanke.com/course/476
Ignore
https://www.taobao.com/
Ignore
https://www.jisuanke.com/course/476
https://www.jisuanke.com/course/429
Ignore
https://www.jisuanke.com/course/476
数据规模与约定
0
<
n
≤
100000
0<n≤100000
0<n≤100000,
网址长度小于100。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; // 操作次数
cin >> n;
string op; // 操作类型
string wang; // 网址
stack<string> q; // 栈存储网址
bool isB = 1; // 是否可以回退
bool isQ = 0; // 是否可以前进
string next; // 存储前进一次后的网址
while (n--)
{
cin >> op;
if (op == "VISIT")
{
cin >> wang;
cout << wang << '\n';
q.push(wang);
isQ = 0; // 输入网址后无法前进
isB = 1; // 输入网址后可以回退(但是后面要特判q.size()<2不能回退)
}
else if (op == "BACK")
{
if (isB == 0 || q.size() < 2) // isB为0或者q.size()<2下无法回退
{
cout << "Ignore" << '\n';
continue;
}
next = q.top(); // 回退时要先记录next网址
q.pop();
cout << q.top() << '\n'; // 输出回退后的网址
isQ = 1; // 回退后可前进
isB = 0; // 回退后不可再次回退
}
else
{
isB = 1; // 读入前进操作后可回退
if (isQ)
{
q.push(next);
isQ = 0; // 前进后不可前进
cout << q.top() << '\n';
}
else
cout << "Ignore" << '\n';
}
}
return 0;
}