Woburn Challenge 2017-18 Round 3 - Junior Division

As part of your internship at a popular meme generation site, you've been tasked with implementing a new ASCII art feature!
You're given a grid of non-whitespace characters with rows and
columns
, representing a meme image. The user will
then be able to specify two pieces of custom text to overlay onto the
image, near its top and bottom edges, in order to enhance its comedic
effect.
The user will first specify a non-empty string with length no
greater than
. Each of its characters will be either an uppercase
letter or an underscore, and it will neither start nor end with an
underscore. This string should be laid on top of the image in the second
row from the top, and horizontally centered. If it can't be perfectly
centered (for example, if its length is odd while
is even), then it
should instead be placed slightly to the left, as close to centered as
possible. Underscores should be omitted, allowing the original image's
characters to show through at those locations instead.
Finally, the user will specify a non-empty string with the same
constraints as
. It should similarly be laid on top of the image in
the second row from the bottom, and horizontally centered.
Your task is to generate the resulting image after both strings
and
have been laid on top of it, and give it back to the user,
so that they can go post it on various social media platforms and obtain
millions of well-deserved upvotes for their original, creative comedic
content.
Input Specification
The first line of input consists of two space-separated integers,
and
.
lines follow, the
-th of which consists of
characters
representing the
-th row of the image grid, for
.
The next line consists of a single string, .
The next line consists of a single string, .
Output Specification
Output lines with
characters per line, the
-th of which
should be the
-th row of the updated image grid.
Sample Input
17 28
........._.............__...
........|+\.........../++|..
........|__\______.../+++|..
......./==========\_/++++|..
....._/=============\++++|..
..._/================\_++|..
../===/#\==============\_|..
..|==\##/====/##\========\..
.|===========\##/=========\.
.|=================++++====|
|====####=========++++++===|
|===|####=========++++++===|
|===\###___/======++++++===|
|==================++++====|
.|========================|.
..\=======================|.
...\=====================/..
VERY_ART
WOW
Sample Output
........._.............__...
........|+VERY.ART..../++|..
........|__\______.../+++|..
......./==========\_/++++|..
....._/=============\++++|..
..._/================\_++|..
../===/#\==============\_|..
..|==\##/====/##\========\..
.|===========\##/=========\.
.|=================++++====|
|====####=========++++++===|
|===|####=========++++++===|
|===\###___/======++++++===|
|==================++++====|
.|========================|.
..\=========WOW===========|.
...\=====================/..
Sample Explanation
Much ASCII.
Comments