POJ-1028Web Navigation,大水题坑我3遍

来源:互联网 发布:mac能pdf能转换成ppt吗 编辑:程序博客网 时间:2024/04/30 00:48

Web Navigation
Time Limit: 1000MS Memory Limit: 10000KTotal 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
原创粉丝点击