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:

  1. 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.
  2. 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.
  3. 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.
  4. 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 (≤ 100), denoting the number of test cases.

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

题目意思:利用栈模拟一下浏览器访问网页的过程。

解题思路:建立两个栈,分别储存向前和向后的元素,其实就是在来回倒。

唉,好久没做题了,碰上这一道栈的题,来回做了好久,不过也是在补之前的漏洞,之前就碰到过这样一道双栈的题目,然而并没有及时补题,拖到了现在。

 #include<iostream>
#include<stack>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main()
{
int t,count=;
string x,y;
scanf("%d",&t);
while(t--)
{
printf("Case %d:\n",count++);
stack<string>s1;
stack<string>s2;
s1.push("http://www.lightoj.com/");
while()
{
cin>>x;
if(x[]=='Q')
{
break;
}
else if(x[]=='V')
{
cin>>y;
s1.push(y);
cout<<y<<endl;
while(!s2.empty())///清空
{
s2.pop();
}
}
else if(x[]=='B')///因为s1栈顶元素是当前访问的页面,后退一步必须返回当前栈顶的下一个元素。
{
if(s1.size()>)///此时栈内必须有两个以上的元素
{
s2.push(s1.top());
s1.pop();///删掉当前的页面
cout<<s1.top()<<endl;
}
else
{
cout<<"Ignored"<<endl;
}
}
else if(x[]=='F')
{
if(!s2.empty())
{
s1.push(s2.top());
cout<<s2.top()<<endl;
s2.pop();///删掉当前的页面
}
else
{
cout<<"Ignored"<<endl;
}
}
}
}
return ;
}
05-11 22:54