SGU - 271

双端队列。
大于k之后的数可以存在一个栈中,这部分的数不会改变顺序。

#include <bits/stdc++.h>
#define decode(x) (x - 'A' + 1)

using namespace std;

typedef long long ll;
typedef char word[20];

ll Hash(char *A){
	ll h = 0;
	for(int i = 0; A[i]; i++)	h = h * 100 + decode(A[i]);
	return h;
}

void get_word(ll h){
	if(h)	get_word(h / 100), putchar(h % 100 + 'A' - 1);
}

int n, m, k;

int main(void)
{
	 while(~scanf("%d%d%d", &n, &m, &k)){
	 	deque<ll> q;	deque<ll> tail;
	 	int dir = 0;
		// 0 Left
		// 1 Right
	 	for(int i = 0; i < n; i++){
	 		word temp;
	 		scanf("%s", temp);
	 		if(!dir)	q.push_back(Hash(temp));
	 		else 		q.push_front(Hash(temp));
	 		if(q.size() > k){
	 			ll pop;
	 			if(!dir)	pop = q.back(), q.pop_back();
	 			else 		pop = q.front(), q.pop_front();
	 			tail.push_back(pop);
	 		}
	 	}
	 	while(m--){
			word opera;
			scanf("%s", opera);
			if(opera[0] == 'A'){
				word temp;
				for(int i = 4; ; i++){
					if(isalpha(opera[i]))	temp[i - 4] = opera[i];
					else{
						temp[i - 4] = '\0';
						break;
					}
				}
				if(!dir)	q.push_front(Hash(temp));
	 			else 		q.push_back(Hash(temp));
	 			if(q.size() > k){
	 				ll pop;
	 				if(!dir)	pop = q.back(), q.pop_back();
	 				else 		pop = q.front(), q.pop_front();
	 				tail.push_front(pop);
	 			}
			}else dir ^= 1;	 		
	 	}
	 	while(!q.empty()){
	 		if(!dir)	get_word(q.front()), q.pop_front();
	 		else		get_word(q.back()), q.pop_back();
			printf("\n"); 
	 	}
	 	while(!tail.empty()){
	 		get_word(tail.front());	tail.pop_front();
	 		printf("\n");
	 	}
	 }
	 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值