1643: WEB浏览器
Time Limit: 1 Sec Memory Limit: 65535 MB 64bit IO Format: %lldSubmitted: 193 Accepted: 126
[ Submit][ Status][ Web Board] [ Edit] [ TestData]
Description
模拟一个网页浏览的过程,给出起始页面,有三种命令:
(1)VISIT url:访问某URL,当前页面变为该URL。并且清空此页面后的所有浏览页面。
(2)BACK:后退,倒退到前一个网页
(3)FORWARD:前进,同理如上
(4)QUIT:退出,结束整个命令输入,这一行输入不用做任何处理,结束程序即可
假定浏览器最初的访问页面为:http://acm.wust.edu.cn/
Input
包含一系列指令(FORWARD,BACK,VISIT,QUIT),URL地址质检部包括空白符,其长度不超过70个字符。假定2个栈任何时候都不超过10个URL地址。最后输入QUIT表示关闭浏览器,退出程序。
Output
对每一个指令(QUIT除外)均输出当前访问页面URL地址。如果出现意外情况,则输出“Ignored”。
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://acm.wust.edu.cn/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://acm.wust.edu.cn/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored
题解:
这题一开始用dfs写,也就是递归栈,后来写得觉得越来越不对劲就用了静态栈写,wa了一发,把样例画了个图,理解了forward的真正含义以后就ac了
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
#define lson k*2
#define rson k*2+1
#define M (t[k].l+t[k].r)/2
#define ll long long
char com[4][30]={"VISIT","BACK","FORWARD","QUIT"};
int maxx;
struct Stack
{
int topp;
char s[1005][105];
Stack()
{
topp=0;
}
void pop()
{
topp--;
}
void push(char t[])
{
strcpy(s[topp],t);
topp++;
}
int empty()
{
if(topp==0)
return 1;
return 0;
}
char* top()
{
return s[topp-1];
}
char* forward()
{
topp++;
return s[topp-1];
}
}s;
int main()
{
//freopen("output.txt","w",stdout);
maxx=-1;
s.push("http://acm.wust.edu.cn/");
char say[105],url[105];
while(1)
{
scanf("%s",say);
if(strcmp(say,com[0])==0)
{
getchar();
gets(url);
printf("%s\n",url);
s.push(url);
}
else if(strcmp(say,com[1])==0)
{
if(s.topp>1)
{
s.pop();
maxx=s.topp+1;
printf("%s\n",s.top());
}
else
printf("Ignored\n");
}
else if(strcmp(say,com[2])==0)
{
if(s.topp<=maxx)
printf("%s\n",s.forward());
else
printf("Ignored\n");
}
else
break;
}
return 0;
}