it almost seems to me that you want to have a list of links ordered from longest substitution to shortest