題目鏈接:http://www.spoj.com/problems/ADAQUEUE/
題意:就是讓你模擬一個雙端隊列的front(),back(),push_back(),push_front(),reverse這些功能。
分析:這題,如果像我當時一樣就很單純直接往上面貼函數reverse就肯定會T,正確的做法是用一個標記來保存看他是否已經reverse了,所謂reverse就是頭變尾,尾變頭。感覺自己還是naive啊。
code:
#include<algorithm>
#include<deque>
#include<cstdio>
using namespace std;
char order[20];
int main(){
int Q;scanf("%d",&Q);
deque<int>dq;
bool hasRev=false;//標記是否已經reverse
while(Q--){
scanf("%s",order);
if(dq.empty()){
if(order[0]=='b'||order[0]=='f'){
printf("No job for Ada?\n");
continue;
}
}
if(order[0]=='b'){
if(!hasRev){
printf("%d\n",dq.back());
dq.pop_back();
}
else{
printf("%d\n",dq.front());
dq.pop_front();
}
}
if(order[0]=='f'){
if(!hasRev){
printf("%d\n",dq.front());
dq.pop_front();
}
else{
printf("%d\n",dq.back());
dq.pop_back();
}
}
if(order[0]=='r')hasRev=!hasRev;//反轉
if(order[0]=='p'){
int num; scanf("%d",&num);
if(!hasRev) dq.push_back(num);
else dq.push_front(num);
}
if(order[0]=='t'){
int num; scanf("%d",&num);
if(!hasRev)dq.push_front(num);
else dq.push_back(num);
}
}
}