sdut 1466 双向队列
生活随笔
收集整理的這篇文章主要介紹了
sdut 1466 双向队列
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
雙向隊列
Time Limit:?1000MS?Memory Limit:?65536KB Submit?Statistic?DiscussProblem Description
? ? ? 想想雙向鏈表……雙向隊列的定義差不多,也就是說一個隊列的隊尾同時也是隊首;兩頭都可以做出隊,入隊的操作。
現(xiàn)在給你一系列的操作,請輸出最后隊列的狀態(tài);
命令格式:
LIN?X??X表示一個整數(shù),命令代表左邊進隊操作;
RIN?X??表示右邊進隊操作;
ROUT
LOUT???表示出隊操作;
Input
第一行包含一個整數(shù)M(M<=10000),表示有M個操作;
以下M行每行包含一條命令;
命令可能不合法,對于不合法的命令,請在輸出中處理;
Output
輸出的第一行包含隊列進行了M次操作后的狀態(tài),從左往右輸出,每兩個之間用空格隔開;
以下若干行處理不合法的命令(如果存在);
對于不合法的命令,請輸出一行X?ERROR
其中X表示是第幾條命令;
Example Input
8 LIN 5 RIN 6 LIN 3 LOUT ROUT ROUT ROUT LIN 3Example 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 ; }《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的sdut 1466 双向队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sdut 3335 数据结构实验之栈与队
- 下一篇: sdut 1479 数据结构实验之栈与队