http://tjuacm.chaosheng.top/problem.php?id=1255
一开始没通过是因为访问不到url时就直接把cur设成ignored了,这样会导致“ignored”被当作url压入栈,改了之后就ac了。通过的代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <stack> using namespace std; stack<string> back; stack<string> forw; int main(){ string command, url, cur; cur = "http://www.acm.org/"; while(cin >> command){ if(command == "QUIT") break; if(command == "VISIT"){ cin >> url; while(!forw.empty()) forw.pop(); back.push(cur); cur = url; cout << cur << endl; } if(command == "BACK"){ if(back.empty()){ cout << "Ignored" << endl; }else{ string tmp = back.top(); back.pop(); forw.push(cur); cur = tmp; cout << cur << endl; } } if(command == "FORWARD"){ if(forw.empty()){ cout << "Ignored" << endl; }else{ string tmp = forw.top(); forw.pop(); back.push(cur); cur = tmp; cout << cur << endl; } } } return 0; }
一开始没通过的:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <stack> using namespace std; stack<string> back; stack<string> forw; int main(){ string command, url, cur; cur = "http://www.acm.org/"; while(cin >> command){ if(command == "QUIT") break; if(command == "VISIT"){ cin >> url; while(!forw.empty()) forw.pop(); back.push(cur); cur = url; } if(command == "BACK"){ if(back.empty()){ cur = "Ignored"; }else{ string tmp = back.top(); back.pop(); forw.push(cur); cur = tmp; } } if(command == "FORWARD"){ if(forw.empty()){ cur = "Ignored"; }else{ string tmp = forw.top(); forw.pop(); back.push(cur); cur = tmp; } } cout << cur << endl; } return 0; }