题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1028 弄2个栈进行操作即可,注意在输入 VISIT时 forward stack 要清空。 #include <iostream> #include <stack> #include <string> using namespace std; int main(){ stack<string> back,forward; string str,s; while(cin>>str&&str!="QUIT"){ if(back.empty()) back.push("http://www.acm.org/"); if(str=="VISIT"){ cin>>s; cout<<s<<endl; back.push(s); while(!forward.empty()) forward.pop(); } else if(str=="BACK"){ if(back.size()>1) forward.push(back.top()); back.pop(); if(back.empty()) cout<<"Ignored"<<endl; else cout<<back.top()<<endl; } else if(str=="FORWARD"){ if(forward.empty()) cout<<"Ignored"<<endl; else { cout<<forward.top()<<endl; back.push(forward.top()); forward.pop(); } } } return 0; }