Improve the implementation for finding the longest common prefix.