我覺得用perl寫最簡單
為求易讀性所以我把許多perl magic都拿掉了
熟悉perl的話是可以把它寫得更簡潔的
#!/usr/bin/env perl
use 5.010;
do {
$_ = <STDIN>;
chomp $_; # chomp removes trailing \n
push @arr, $_;
} until ($_ =~ /flag/);
$flag = pop @arr; # the text match flag
$_ = <STDIN>; # obtain the next line after flag
print "$flag $_";
while ($_=<STDIN>) {
print ((shift @arr), " ", $_);
};