题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=61
用容器stack,剩下的就是模拟了。
#include<iostream> #include<stack> #include<string> using namespace std; stack<string> Front; //前进栈 stack<string> Back; //后退栈 string URL; //当前页 void BACK(string order) { if (Back.empty()) { cout<<"Ignored"<<endl; return; } order = order.erase(0,4); Front.push(URL); URL = Back.top(); Back.pop(); cout<<URL<<endl; } void FORWARD(string order) { if (Front.empty()) { cout<<"Ignored"<<endl; return; } order = order.erase(0,7); Back.push(URL); URL = Front.top(); Front.pop(); cout<<URL<<endl; } void VISIT(string order) { order = order.erase(0,6); Back.push(URL); URL = order; cout<<URL<<endl; while (!Front.empty()) Front.pop(); } int main() { int N; cin>>N; while (N--) { string order; bool Exit = false; URL = "http://www.acm.org/"; while (getline(cin,order) && order!="QUIT") { switch (order[0]) { case 'B':BACK(order);break; case 'F':FORWARD(order);break; case 'V':VISIT(order);break; } } while (!Front.empty()) Front.pop(); while (!Back.empty()) Back.pop(); if (N) { cin.ignore(); cout<<endl; } } return 0; }