optimize memory footprint of cpp solution to codechef trsubtr