Trie based memoization that actually passes SPOJ