Perl 5 + -pap
, 4436 bytes
%a=map%a=<>=~/(.\S)(.*\S*)/g,@F;$_=<>;sg;s/(.)\K/$a$&$a{lc$1lc$&}/g
Explanation
Using @F
(populated and -a
uto-split on whitespace from the first line of input that contains the segments), theThe hash %a
is built using the first letter of each segment (from <>
, STDIN
) as the key and any remaining letters (0 or more) as the value. Then $_
is populated with the next line of input and a s///
ubstitution is performed on $_
replacing each character in the string with itself (but \K$&
eeping it in place) withfollowed by the value from the hash at (the l
owerc
ase) key in %a
. $_
is then implicitly -p
rinted.