双端队列。
大于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;
}