現(xiàn)在給你一系列的操作,請輸出最后隊(duì)列的狀態(tài); 命令格式: LIN X X表示一個整數(shù),命令代表左邊進(jìn)隊(duì)操作; RIN X 表示右邊進(jìn)隊(duì)操作; ROUT LOUT 表示出隊(duì)操作; Input
第一行包含一個整數(shù)M(M<=10000),表示有M個操作; 以下M行每行包含一條命令; 命令可能不合法,對于不合法的命令,請?jiān)谳敵鲋刑幚恚?Output
輸出的第一行包含隊(duì)列進(jìn)行了M次操作后的狀態(tài),從左往右輸出,每兩個之間用空格隔開; 以下若干行處理不合法的命令(如果存在); 對于不合法的命令,請輸出一行X ERROR 其中X表示是第幾條命令; Example Input
8LIN 5RIN 6LIN 3LOUTROUTROUTROUTLIN 3Example Output
37 ERRORHint
Author wanglin
#include <iostream>#include <algorithm>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <bits/stdc++.h>#include <stack>#include <deque>using namespace std;int main(){ int p1[10000]; int i=0; deque <int > p; char g[10]; int m; scanf("%d", &m); for(int a=1; a<=m; a++) { scanf("%s", g); if(strcmp(g, "LIN")==0) { int k; scanf("%d", &k); p.push_front(k); } else if(strcmp(g, "RIN")==0) { int k; scanf("%d", &k); p.push_back(k); } else if(strcmp(g, "ROUT")==0) { if(p.empty()) { p1[i++]=a; } else p.pop_back(); } else if(strcmp(g, "LOUT")==0) { if(p.empty()) { p1[i++]=a; } else p.pop_front(); } } int top=1; while(!p.empty()) { if(top)top=0; else printf(" "); printf("%d", p.front()); p.pop_front(); } printf("/n"); for(int a=0; a<i; a++) { printf("%d ERROR/n", p1[a]); } return 0;}
|
新聞熱點(diǎn)
疑難解答