POJ-1028Web Navigation

Web Navigation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 32963 Accepted: 14704
                                                             ->   Link   <-

   本来这种题意一目了然的题就得一遍过的,无奈很多小细节出了问题,表示只要照着题目告诉你的写就可以过了;

   还有英语是硬伤!!!

   题意:一个浏览器初始网址是”http://www.acm.org/“,当浏览新网页时就会有历史记录(就用我们平时用的来讲吧更好理解),我们回去浏览某个历史网页时刚刚浏览的网页就变成前一个网页了,(比如UC右键右滑是前进,左滑是后退),当我们浏览的是最新打开的网页时前进是无效的,而后退有效,当退回初始页面时再后退也无效了,反而前进有效回到刚才的页面;本题就是这个意思,然后输入一连串指令要我们求出当前网页网址;

   思路:实际上题目已经告诉我们怎么做了;开两个栈,一个backward,一个forward,操作如下:

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. 后退操作:先判断backward栈是否为空,是则忽视此操作,否则将当前页面存入forward栈中,然后将backward栈顶取出成为当前页面;
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. 前进操作:先判断forward栈是否为空,是则忽视此操作,否则将当前页面存入backward栈中,然后将forward栈顶取出成为当前页面;
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. 输入结束标志;

    只要操作有效(前进或后退不为空)则输出当前页面;

string ask,cur;
int main()
{
    stack<string>before;
    stack<string>ward;
    cur="http://www.acm.org/";
    while(cin>>ask&&ask[0]!='Q')
    {
        if(ask[0]=='V')打开新网页;
        {
            ward.push(cur);
            while(!before.empty())//前进无效了,所以before栈清空;
                before.pop();
            cin>>cur;
            cout<<cur<<endl;
        }
        else if(ask[0]=='B')
        {
            if(ward.empty())//先判断是否为空,不然WA,博主就是跪在这里;
                cout<<"Ignored"<<endl;
            else
            {
                before.push(cur);
                cur=ward.top();
                ward.pop();
                cout<<cur<<endl;
            }
        }
        else if(ask[0]=='F')
        {
            if(before.empty())
                cout<<"Ignored"<<endl;
            else
            {
                ward.push(cur);
                cur=before.top();
                before.pop();
                cout<<cur<<endl;
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值