algorithm - Using Double Ended Queues (Deques) for Transposing sequences -
this interesting question might more interesting useful. want copy 2 deques(dq1_a
, dq1_b
) copies (dq2_a
, dq2_b
).
dq1_a= <a,b,c,d,e,f> dq1_b= <g,h,i,j,k,l,m>
but want exchange "strands" @ d<->j
. want end
dq2_a= <a,b,c,(d or j),k,l,m> dq2_b= <g,h,i,(j or d),e,f>
the or
exists because depends on whether include elements marking exchange in or not.
question 1
: find algorithm end transposed datastructure dq2_*
starting given deques dq1_*
, exchanged elements ex=(d<->j); ex[a]=d; ex[b]=j
. allowed deque operations - left_push,left_pop,right_push,right_pop,deque_empty
(last 1 returns whether deque empty or not).
solution:
left_item <- left_pop(dq1_a) while (left_item!=ex[a]): right_push(dq2_a,left_item) left_item<-left_pop(dq1_a) right_item <- right_pop(dq1_b) while (right_item!=ex[b]): left_push(dq2_b,right_item) right_item<-right_pop(dq1_b) # if not inclusive right_push(dq2_a,left_item) left_push(dq2_b,right_item) # else #right_push(dq2_b,left_item) #left_push(dq2_a,right_item) left_item <- left_pop(dq1_b) while (not deque_empty(dq1_b)): right_push(dq2_a,left_item) left_item<-left_pop(dq1_a) right_item <- right_pop(dq1_a) while (not deque_empty(dq1_a)): left_push(dq2_b,right_item) right_item<-rightt_pop(dq1_a)
question 2
: beats me. given set of dq1_*
(more 2) - have copy them dq2_*
using list of exchanges
ex={s: s given set of exchanges of form: (ith element of dq1_x)<->(jth element of dq1_y)}
any better solutions problem welcome!!
Comments
Post a Comment