题意
这道题是一个网页浏览模拟,在进行访问,前进和后退操作的时候,输出当前网页。如果无法前进或者后退,便输出Ignored。
分析
考虑一下,浏览网页的操作类似于对栈的操作。这里用一个数组来模拟栈,同时设置一个索引来指示当前位置,每visit一个网页,只需要将当前位置网页覆盖即可,同时丢弃后面的网页。
代码如下:
Memory: 124K Time: 0MS Length: 33LINES
#include<iostream>
char vecWeb[200][71];
int main()
{
strcpy(vecWeb[0], "http://www.acm.org/\n");
char tmp[77];
int pos = 0;
int size = 1;
while (fgets(tmp, 77, stdin) && strcmp(tmp, "QUIT\n"))
{
if (tmp[5] == ' ')
{
++pos;
size = pos + 1;
strcpy(vecWeb[pos], tmp + 6);
printf("%s", vecWeb[pos]);
}
else
{
if (!strcmp(tmp, "BACK\n"))
{
if (pos > 0) printf("%s", vecWeb[--pos]);
else printf("Ignored\n");
}
else
{
if (pos < size - 1) printf("%s", vecWeb[++pos]);
else printf("Ignored\n");
}
}
}
return 0;
}