#!/usr/bin/perl
use 5.016;
use warnings;
sub indx
{ (grep{ $_[0]->[$_] == $_[1] } (0 .. $#{$_[0]}))[0] }
sub f {
my ($seq_a, $am, $seq_b) = @_;
my $i = indx($seq_a, $am);
@index{@{$seq_a}[$i .. $#{$seq_a}, 0 .. $i - 1]} = (0 .. $#{$seq_a});
return [sort{ $index{$a} <=> $index{$b} } @{$seq_b}]; }
use Data::Dumper;
local $Data::Dumper::Indent = 0; local $Data::Dumper::Terse = 1;
say Dumper f([2, 3, 5, 7, 9], 2, [9, 2, 5]);
say Dumper f([2, 3, 5, 7, 9], 9, [9, 2, 5]);
say Dumper f([2, 3, 5, 7, 9], 5, [9, 2, 5]);
say Dumper f([2, 3, 5, 7, 9], 3, [9, 2, 5, 5, 2]);
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwoKc3ViIGluZHggeyAoZ3JlcHsgJF9bMF0tPlskX10gPT0gJF9bMV0gfSAoMCAuLiAkI3skX1swXX0pKVswXSB9CgpzdWIgZiB7CglteSAoJHNlcV9hLCAkYW0sICRzZXFfYikgPSBAXzsKCglteSAkaSA9IGluZHgoJHNlcV9hLCAkYW0pOwoKCW15ICVpbmRleDsKCUBpbmRleHtAeyRzZXFfYX1bJGkgLi4gJCN7JHNlcV9hfSwgMCAuLiAkaSAtIDFdfSA9ICgwIC4uICQjeyRzZXFfYX0pOwoKCXJldHVybiBbc29ydHsgJGluZGV4eyRhfSA8PT4gJGluZGV4eyRifSB9IEB7JHNlcV9ifV07Cn0KCnVzZSBEYXRhOjpEdW1wZXI7CmxvY2FsICREYXRhOjpEdW1wZXI6OkluZGVudCA9IDA7CmxvY2FsICREYXRhOjpEdW1wZXI6OlRlcnNlID0gMTsKCnNheSBEdW1wZXIgZihbMiwgMywgNSwgNywgOV0sIDIsIFs5LCAyLCA1XSk7CnNheSBEdW1wZXIgZihbMiwgMywgNSwgNywgOV0sIDksIFs5LCAyLCA1XSk7CnNheSBEdW1wZXIgZihbMiwgMywgNSwgNywgOV0sIDUsIFs5LCAyLCA1XSk7CnNheSBEdW1wZXIgZihbMiwgMywgNSwgNywgOV0sIDMsIFs5LCAyLCA1LCA1LCAyXSk7Cg==