1378 雙向隊列

來源:互聯網
上載者:User
 
描述

想想雙向鏈表……雙向隊列的定義差不多,也就是說一個隊列的隊尾同時也是隊首;兩頭都可以做出隊,入隊的操作。

現在給你一系列的操作,請輸出最後隊列的狀態;

命令格式:

LIN X X表示一個整數,命令代表左邊進隊操作;

RIN X 表示右邊進隊操作;

ROUT

LOUT 表示出隊操作;

輸入

第一行包含一個整數M,表示有M個操作;

以下M行每行包含一條命令;

命令可能不合法,對於不合法的命令,請在輸出中處理;

輸出

輸入第一行包含隊列進行了M次操作後的狀態,從左往右輸出;

以下若干行處理不合法的命令(如果存在);

對於不合法的命令,請輸出一行X ERROR

其中X表示是第幾條命令;

範例輸入
 
8
LIN 5
RIN 6
LIN 3
LOUT
ROUT
ROUT
ROUT
LIN 3
範例輸出
3
7 ERROR

類比題,隊列

#include<stdio.h>int a[4000];int b[4000];int flag[3000];main(){int number;char t,tt;int upset;int up1,up2;int length;int j;int up3;int ttttt;scanf("%d",&number);up1=up2=0;length=0;up3=0;ttttt=number;while(number--){getchar();scanf("%c%c",&t,&tt);if(t=='L'&&tt=='I'){scanf("%c%c",&t,&tt);scanf("%d",&upset);a[up1++]=upset;length++;}if(t=='R'&&tt=='I'){scanf("%c%c",&t,&tt);scanf("%d",&upset);b[up2++]=upset;length++;}if(t=='L'&&tt=='O'){scanf("%c%c",&t,&tt);if(length>0){if(up1>0){ up1--;length--;}else{for(j=0;j<up2-1;j++)b[j]=b[j+1];up2--;length--;}}else{flag[up3++]=ttttt-number;}}if(t=='R'&&tt=='O'){scanf("%c%c",&t,&tt);if(length>0){if(up2>0){ up2--;length--;}else{for(j=0;j<up1-1;j++)a[j]=a[j+1];up1--;length--;}}else{flag[up3++]=ttttt-number;}}}   for(j=up1-1;j>=0;j--)   printf("%d ",a[j]);   for(j=0;j<up2;j++)   printf("%d ",b[j]);   printf("\n");   for(j=0;j<up3;j++)   printf("%d ERROR\n",flag[j]);}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.