您的位置:首页 > 其它

POJ 1028-Web Navigation

2017-12-02 18:53 423 查看
Web Navigation

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 34867 Accepted: 15579
题目链接:点击打开链接

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. 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

题意:
本题的题意很好理解,让你模仿一个栈,有四个命令BACK, FORWARD,VISIT,QUIT,本题是让你访问网站,最初你的网站地址是http://www.acm.org/,然后VISIT,这个命令后面会跟上你要访问的网址,然后对应将它输出,BACK这个命令是返回,让你输出当前网站的上一个网站地址,FORWARD这个命令是前进,是让你输出当前命令的下一个网址,对于BACK,FORWARD这两个命令,如果没有满足条件的网站就输出Ignored,然后QUIT是输入的结束,还有一点需要注意,就是如果在你返回到某一个网站地址的时候,你再用VISIT命令新访问一个网站的时候,那么以前该网站前面的网站都被抹去。

分析:
题意只要知道了,感觉心里就有数了,我用数组水过的,慢慢控制,一点点调改好的。

#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;

char s[1005][75];///存网址
int main()
{
char h[10];///存命令
int k,bj=0;
///bj是跟着新出现的网址走的,统计我们此时有多少个网址
k=1;///保存我们当前的网站的位置
memset(s,'0',sizeof(s));///这个一定要有,不然不好判断
while(~scanf("%s",h))
{
if(strcmp(h,"QUIT")==0)///输入结束标志
break;
else if(strcmp(h,"VISIT")==0)///访问新网站
{
scanf("%s",s[k++]);
bj++;
printf("%s\n",s[k-1]);
for(int i=k;i<=bj;i++)///将前面的网站都抹去
s[i][0]='0';
}
else if(strcmp(h,"BACK")==0)///查找该网站的上一个网站
{
k--;
if((k-1)<0)///说明超出边界
{
printf("Ignored\n");
k=1;
}
else if((k-1)==0)///说明该网站的上一个网站是最初的网站
printf("http://www.acm.org/\n");
else
printf("%s\n",s[k-1]);
}
else if(strcmp(h,"FORWARD")==0)///查找该网站的下一个网站
{
k++;
if(s[k-1][0]=='0')///说明该网站的下一个网站没有
{
printf("Ignored\n");
k--;
}
else if((k-1)==0)///说明是最初的网站
printf("http://www.acm.org/\n");
else
printf("%s\n",s[k-1]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: