spoj Ada and Queue 模拟+deque

题目链接:http://www.spoj.com/problems/ADAQUEUE/

题意:就是让你模拟一个双端队列的front(),back(),push_back(),push_front(),reverse这些功能。

分析:这题,如果像我当时一样就很单纯直接往上面贴函数reverse就肯定会T,正确的做法是用一个标记来保存看他是否已经reverse了,所谓reverse就是头变尾,尾变头。感觉自己还是naive啊。

code:

#include<algorithm>
#include<deque>
#include<cstdio>
using namespace std;
char order[20];
int main(){
    int Q;scanf("%d",&Q);
    deque<int>dq;
    bool hasRev=false;//标记是否已经reverse
    while(Q--){
        scanf("%s",order);
        if(dq.empty()){
            if(order[0]=='b'||order[0]=='f'){
                printf("No job for Ada?\n");
                continue;
            }
        }
        if(order[0]=='b'){
            if(!hasRev){
                printf("%d\n",dq.back());
                dq.pop_back();
            }
            else{
                printf("%d\n",dq.front());
                dq.pop_front();
            }
        }
        if(order[0]=='f'){
            if(!hasRev){
                printf("%d\n",dq.front());
                dq.pop_front();
            }
            else{
                printf("%d\n",dq.back());
                dq.pop_back();
            }
        }
        if(order[0]=='r')hasRev=!hasRev;//反转
        if(order[0]=='p'){
           int num; scanf("%d",&num);
          if(!hasRev) dq.push_back(num);
          else dq.push_front(num);
        }
        if(order[0]=='t'){
           int num; scanf("%d",&num);
           if(!hasRev)dq.push_front(num);
           else dq.push_back(num);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值