Build a prefix tree for X and a suffix tree for Y...