如题:http://poj.org/problem?id=1028
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30623 | Accepted: 13717 |
Description
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
Output
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
Source
思路:水的抠脚。
要注意的是我一开始使用stl 的stack。stack<char *>s. 其中的参数传的其实是指针变量,值随着变量的变化变化。因此手写栈比较好。所以题目说了栈上限100.
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct node
{
char dat[100];
node(){}
node(char t[100])
{
strcpy(dat,t);
}
};
int main()
{
// freopen("C:\\1.txt","r",stdin);
char s[10];
char cur[100]="http://www.acm.org/";
node b_stack[120];
node f_stack[120];
int b_top=0;
int f_top=0;
while(cin>>s)
{
if(s[0]=='Q')
break;
if(s[0]=='V')
{
f_top=0;
b_stack[b_top++]=node(cur);
char t[100];
cin>>t;
strcpy(cur,t);
}
else if(s[0]=='B')
{
if(b_top==0)
{
printf("Ignored\n");
continue;
}
else
{
f_stack[f_top++]=node(cur);
strcpy(cur,b_stack[--b_top].dat);
}
}
else
{
if(f_top==0)
{
printf("Ignored\n");
continue;
}
else
{
b_stack[b_top++]=node(cur);
strcpy(cur,f_stack[--f_top].dat);
}
}
printf("%s\n",cur);
}
return 0;
}