日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sdut 1466 双向队列

發布時間:2025/4/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sdut 1466 双向队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

雙向隊列

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

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();elsede.pop_back();}deque<int>::iterator it;for(it=de.begin();it!=de.end();it++){if(it==de.begin())cout<<*it;elsecout<<' '<<*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 ;}elseq.pop_front() ;}else if(strcmp(ch,"ROUT") == 0){if(q.empty())flag[i] = 1 ;elseq.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 ; }

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的sdut 1466 双向队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。