Time Limit: 1 second(s) | Memory Limit: 32 MB |
Standard web browsers contain features to move backward andforward among the pages recently visited. One way to implement these featuresis to use two stacks to keep track of the pages that can be reached by moving backward and forward. You are asked toimplement this. The commands are:
1. BACK:If the backward stack is empty, the command is ignored. Otherwise, push thecurrent page on the top of the forward stack. Pop the page from the top of thebackward stack, making it the new current page.
2. FORWARD:If the forward stack is empty, the command is ignored. Otherwise, push thecurrent page on the top of the backward stack. Pop the page from the top of theforward stack, making it the new current page.
3. VISIT<url>: Push the current page on the top of the backward stack, and makethe URL specified the new current page. The forward stack is emptied.
4. QUIT:Quit the browser.
The browser initially loadsthe 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 haveat most 50 characters. The end of case is indicated by the QUIT command and it shouldn'tbe processed. Each case contains at most 100 lines.
Output
For each case, print the case number first. For eachcommand, print the URL of the current page (in a line) after the command isexecuted if the command is not ignored. Otherwise, print'Ignored'
Sample Input | Output for 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 | 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 |
栈,后进先出,,新技能GET。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
using namespace std;
char str[1000][110];
char s[100];
int main()
{
int t, ncase=1;
scanf("%d", &t);
strcpy(str[0],"http://www.lightoj.com/");
while(t--)
{
printf("Case %d:\n",ncase++);
stack<int>in;
stack<int>out;
while(!in.empty())
{
in.pop();
}
while(!out.empty())
{
out.pop();
}
int cnt=0,now=0;
while(scanf("%s",s))
{
if(strcmp(s,"QUIT")==0)
{
break;
}
if(strcmp(s,"VISIT")==0)
{
cnt++;
scanf("%s",str[cnt]);
in.push(now);
now=cnt;
while(!out.empty())
{
out.pop();
}
printf("%s\n",str[cnt]);
}
else if(strcmp(s,"BACK")==0)
{
if(in.size()>0)
{
out.push(now);
int k=in.top();
now=k;
in.pop();
printf("%s\n",str[k]);
}
else
{
printf("Ignored\n");
}
}
else
{
if(out.size()>0)
{
in.push(now);
int k=out.top();
now=k;
out.pop();
printf("%s\n",str[k]);
}
else
{
printf("Ignored\n");
}
}
}
}
return 0;
}
.