代码改变世界

LightOJ 1212 deque的运用

2012-07-24 10:51  javaspring  阅读(240)  评论(0编辑  收藏  举报

来源:http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=9948#problem/D

题意:就是给你一种队列,两端都能插入,两端都能删除,然后给你一些操作,输出操作的结果。

思路:用deque可以完美实现,题目没什么难度。。。

代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <deque>
using namespace std;

int main(){
	//freopen("1.txt","r",stdin);
	int numcase;
	scanf("%d",&numcase);
    for(int K = 1; K <= numcase; ++K){
	  int numlen,numopera,x;
	  scanf("%d%d",&numlen,&numopera);
	  string ss;
	  deque<int> qq;
	  printf("Case %d:\n",K);
	  while(numopera--){
	    cin>>ss;
		if(ss == "pushLeft"){
			scanf("%d",&x);
			if(qq.size() < numlen){
			   qq.push_front(x);
			   printf("Pushed in left: %d\n",x);
			}
			else{
			  printf("The queue is full\n");
			}
		}
		else if(ss == "pushRight"){
		  scanf("%d",&x);
		  if(qq.size() < numlen){
			  qq.push_back(x);
			  printf("Pushed in right: %d\n",x);
		  }
		  else{
		    printf("The queue is full\n");
		  }
		}
		else if( ss == "popLeft" ){
			if(qq.size() > 0){
			  x = qq.front();
			  qq.pop_front();
			  printf("Popped from left: %d\n",x);
			}
			else{
			  printf("The queue is empty\n");
			}
		}
		else if( ss == "popRight"){
			if(qq.size() > 0){
				x = qq.back();
				qq.pop_back();
				printf("Popped from right: %d\n",x);
			}
			else{
			  printf("The queue is empty\n");
			}
		}
	  }
	}
	return 0;
}