$/=$\;$_=<>; 1while s/(.)\1/$1 $1/; s/^|\B|$/|/mg; /.+/; $m='-'x($L=length$&); s/^/$m\n/mg;s/$/\n$m/; 1while s/((\w).{$L})-(.{$L}\2)/$1 $3/sg ||s/ - / /g ||s/^- | -$/ /m ||s/\|.{$L}\K-|-(?=.{$L}\|)/+/gs ||s/(\|.{$L}) (.{$L}\|)/$1|$2/sg; print
Note that non-ascii characters in the above source code will be escaped (such as \x9f).