sdut 1466 双向队列

来源:互联网 发布:上海大数据开放平台 编辑:程序博客网 时间:2024/06/11 02:17

双向队列

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss

Problem Description

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

Input

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

Output

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

Example Input

8LIN 5RIN 6LIN 3LOUTROUTROUTROUTLIN 3

Example Output

37 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: AcceptedTake time: 12msTake Memory: 260KBSubmit 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 ;}


原创粉丝点击