这道题目的算法题中已经给出来,就是利用两个栈来协同工作,用笔在纸上模拟一翻就可以得出答案,注意的是遇到VISIT操作的时候,最后一步要清空Forward栈。 // Web Navigation.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<iostream> #include<fstream> #include<algorithm> #include<stack> #include<string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { ifstream cin("d://1.txt"); stack<string> back,forward; string s,current="http://www.acm.org/"; while(1){ getline(cin,s); if(s=="QUIT") break; if(s.length()>7){ back.push(current); current=s.substr(6); cout<<current<<endl; while(!forward.empty()) forward.pop(); } else if(s=="BACK"){ if(back.empty()) cout<<"Ignored"<<endl; else{ forward.push(current); current=back.top(); back.pop(); cout<<current<<endl; } } else if(s=="FORWARD"){ if(forward.empty()) cout<<"Ignored"<<endl; else{ back.push(current); current=forward.top(); forward.pop(); cout<<current<<endl; } } } system("pause"); return 0; }