题干:

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. In this problem, you are asked to implement this. 
The following commands need to be supported: 
BACK: 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. If the backward stack is empty, the command is ignored. 
FORWARD: 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. If the forward stack is empty, the command is ignored. 
VISIT : 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. 
Assume that the browser initially loads the web page at the URL http://www.acm.org/

Input

Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command.

Output

For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command.

Sample Input

VISIT http://acm.ashland.edu/
VISIT http://acm.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT

Sample Output

http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored

 

来自一个博客的分析如下:http://www.mamicode.com/info-detail-428744.html

题意简述:
  这题是一个模拟WEB浏览器的程序,默认主页为http://www.acm.org/输入为指令,出书为当前页面的网址。
输入的指令有4种:
  (1)VISIT(后接一个网址)——访问它后面紧接着的网址。
  (2)BACK——访问当前网页的前一个。
  (3)FORWARD——访问当前网页的后一个。
  (4)QUIT——退出(关闭浏览器)。

思路:
  必须记录访问过的网址,因为是顺序关系,所以用数组存储,下标为0的元素为默认页;有一个变量记录当前页面在数组中的位置;BACK和FORWARD指令可能出现特殊情况:
  (1)BACK到默认页面再BACK——输出Ignored。
  (2)FORWARD到最后一个访问到的页面再与FORWARD——输出Ignored。
  这样,就有一个问题:要记录最后一个访问到的页面,对于这个问题只需要在遇到VISIT指令的时候顺便操作上面的指令的对应操作就变得很简单。
  VISIT——就将网址写入数组后再进行输出,记录当前位置,这个位置也即最后一个访问到的页面。
  BACK——当前页面减1,输出网址,注意判断特殊情况。
  FORWARD——当前页面+1,输出网址,也要注意特殊情况。
  QUIT——退出。

 

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;
//stack <string> sk;
char s[1000][100];
int main()
{
// freopen("in.txt","r",stdin);
char op[10];
int top = 0,maxx = 0;
strcpy(s[0],"http://www.acm.org/");
while(scanf("%s",op) ) {
if(!strcmp(op,"QUIT")) break;
else if(op[0] == 'V') {
scanf("%s",s[++top]);
maxx = top;
printf("%s\n",s[top]);
}
else if(op[0] == 'B') {
if(top >= 1) {
top--;
printf("%s\n",s[top]);
}
else if(top == 0) {
printf("Ignored\n");
}
}
else if(op[0] == 'F') {
top++;
if(top>maxx) {
printf("Ignored\n");
top--;
}
else printf("%s\n",s[top]);

}
}

return 0 ;
}

 

AC代码2:(栈)

#include <stdio.h>
#include <string.h>
const int maxn = 75, N = 10005;
char q[N][maxn], queue[N][maxn], str[maxn];
char ch[maxn];
int main ( )
{
int T, top, front, cas = 0;
//freopen ( "in0.in", "r", stdin );
scanf ( "%d", &T );
while ( T -- )
{
front = top = -1;
strcpy ( q[++ top], "http://www.acm.org/" );
//开始就将一个网址加入栈
if ( cas ++ ) //每组数据有换行
printf ( "\n" );
while ( ~ scanf ( "%s", str ) && strcmp ( str, "QUIT" ) != 0 )
{
if ( strcmp ( str, "VISIT" ) == 0 )
{ //查看时将所有可以后退的全部去掉
scanf ( "%s", ch );
printf ( "%s\n", ch );
strcpy ( q[++ top], ch );
front = -1;
}
else if ( strcmp ( str, "BACK" ) == 0 )
{
if ( top <= 0 ) //小于等于0证明不能在后退了
printf ( "Ignored\n" );
else
{
//将此网页加入可以前进的栈中
strcpy ( queue[++ front], q[top] );
printf ( "%s\n", q[-- top] );
}
}
else
{
if ( front < 0 )
printf ( "Ignored\n" );
else
{
//将前进的网页加到后退中
strcpy ( q[++ top], queue[front] );
printf ( "%s\n", queue[front --] );
}
}
}
}
return 0;
}