sdut 1466 双向队列

双向队列

Time Limit: 1000MS  Memory Limit: 65536KB
Problem Description

      想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首;两头都可以做出队,入队的操作。
现在给你一系列的操作,请输出最后队列的状态;
命令格式:
LIN X  X表示一个整数,命令代表左边进队操作;
RIN X  表示右边进队操作;
ROUT
LOUT   表示出队操作;

Input

第一行包含一个整数M(M<=10000),表示有M个操作;
以下M行每行包含一条命令;
命令可能不合法,对于不合法的命令,请在输出中处理;

Output

输出的第一行包含队列进行了M次操作后的状态,从左往右输出,每两个之间用空格隔开;
以下若干行处理不合法的命令(如果存在);
对于不合法的命令,请输出一行X ERROR
其中X表示是第几条命令;

Example Input
8
LIN 5
RIN 6
LIN 3
LOUT
ROUT
ROUT
ROUT
LIN 3
Example Output
3
7 ERROR
 
#include <iostream>
#include <deque>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
    deque<int>de;
    string s;
    int n,t,i,tag[10001]={0};
    cin>>n;
    for( i=1;i<=n;i++)
    {
        cin>>s;
        if(de.empty()&&(s=="LOUT"||s=="ROUT"))
            tag[i]=1;
        else if(s=="LIN")
        {
            cin>>t;
            de.push_front(t);
        }
        else if(s=="RIN")
        {
            cin>>t;
            de.push_back(t);
        }
        else if(s=="LOUT")
            de.pop_front();
        else
            de.pop_back();
    }
        deque<int>::iterator it;
        for(it=de.begin();it!=de.end();it++)
        {
            if(it==de.begin())
            cout<<*it;
            else
                cout<<' '<<*it;
        }
        cout<<endl;
        for(int i=1;i<=n;i++)
           if(tag[i])
            cout<<i<<' '<<"ERROR"<<endl;
    return 0;
}

/***************************************************
User name: YT1658506207邵雪源
Result: Accepted
Take time: 12ms
Take Memory: 260KB
Submit time: 2017-10-12 20:29:16
****************************************************/
转载一篇用queue队列做的
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std ;
int main()
{
    deque<int>q ;
    int M ;
    scanf("%d",&M) ;
    char ch[21] ;
    int a ;
    int flag[100001] ;
    memset(flag,0,sizeof(flag)) ;
    for(int i = 1 ; i <= M ; i++)
    {
        scanf("%s",ch) ;
        if(strcmp(ch,"LIN") == 0)
        {
            scanf("%d",&a) ;
            q.push_front(a) ;
        }
        else if(strcmp(ch,"RIN") == 0)
        {
            cin>>a ;
            q.push_back(a) ;
        }
        else if(strcmp(ch,"LOUT") == 0)
        {
            if(q.empty())
            {
                flag[i] = 1 ;

            }
            else
                q.pop_front() ;
        }
        else if(strcmp(ch,"ROUT") == 0)
        {
            if(q.empty())
                flag[i] = 1 ;
            else
                q.pop_back() ;
        }
    }
    int aa = q.front() ;
    q.pop_front() ;
    printf("%d",aa) ;
    while(!q.empty())
    {
        int aa = q.front() ;
        q.pop_front() ;
        printf(" %d",aa) ;
    }
    cout<<endl ;
    for(int i = 1 ; i <= M ; i++)
    {
        if(flag[i])
            cout<<i<<" ERROR"<<endl ;
    }
    return 0 ;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值