Discover the Web(棧模擬)
Description
Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. You are asked to implement this. The commands are:
- BACK: If the backward stack is empty, the command is ignored. Otherwise, push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page.
- FORWARD: If the forward stack is empty, the command is ignored. Otherwise, push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page.
- VISIT <url>: Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
- QUIT: Quit the browser.
The browser initially loads the web page at the URL ‘http://www.lightoj.com/‘
Input
Input starts with an integer T (
Each case contains some commands. The keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 50 characters. The end of case is indicated by the QUIT command and it shouldn‘t be processed. Each case contains at most 100 lines.
Output
For each case, print the case number first. For each command, print the URL of the current page (in a line) after the command is executed if the command is not ignored. Otherwise, print ‘Ignored‘.
Sample Input
1
VISIT http://uva.onlinejudge.org/
VISIT http://topcoder.com/
BACK
BACK
BACK
FORWARD
VISIT http://acm.sgu.ru/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT
Sample Output
Case 1:
http://uva.onlinejudge.org/
http://topcoder.com/
http://uva.onlinejudge.org/
http://www.lightoj.com/
Ignored
http://uva.onlinejudge.org/
http://acm.sgu.ru/
http://uva.onlinejudge.org/
http://www.lightoj.com/
http://uva.onlinejudge.org/
http://acm.sgu.ru/
Ignored
題目意思:利用棧模擬一下瀏覽器訪問網頁的過程。
解題思路:建立兩個棧,分別儲存向前和向後的元素,其實就是在來回倒。
唉,好久沒做題了,碰上這一道棧的題,來回做了好久,不過也是在補之前的漏洞,之前就碰到過這樣一道雙棧的題目,然而並沒有及時補題,拖到了現在。
1 #include<iostream> 2 #include<stack> 3 #include<algorithm> 4 #include<stdio.h> 5 using namespace std; 6 int main() 7 { 8 int t,count=1; 9 string x,y; 10 scanf("%d",&t); 11 while(t--) 12 { 13 printf("Case %d:\n",count++); 14 stack<string>s1; 15 stack<string>s2; 16 s1.push("http://www.lightoj.com/"); 17 while(1) 18 { 19 cin>>x; 20 if(x[0]==‘Q‘) 21 { 22 break; 23 } 24 else if(x[0]==‘V‘) 25 { 26 cin>>y; 27 s1.push(y); 28 cout<<y<<endl; 29 while(!s2.empty())///清空 30 { 31 s2.pop(); 32 } 33 } 34 else if(x[0]==‘B‘)///因為s1棧頂元素是當前訪問的頁面,後退一步必須返回當前棧頂的下一個元素。 35 { 36 if(s1.size()>1)///此時棧內必須有兩個以上的元素 37 { 38 s2.push(s1.top()); 39 s1.pop();///刪掉當前的頁面 40 cout<<s1.top()<<endl; 41 } 42 else 43 { 44 cout<<"Ignored"<<endl; 45 } 46 } 47 else if(x[0]==‘F‘) 48 { 49 if(!s2.empty()) 50 { 51 s1.push(s2.top()); 52 cout<<s2.top()<<endl; 53 s2.pop();///刪掉當前的頁面 54 } 55 else 56 { 57 cout<<"Ignored"<<endl; 58 } 59 } 60 } 61 } 62 return 0; 63 }
Discover the Web(棧模擬)