If you create two categories named "subcategory" under separate primary categories ("one" and "two", for example), such that the categories are accessible via:
/category/one/category
AND
/category/two/category
WordPress fails to find the second category.
The attached patch attempts to select the category based on the full path of the category, not just the last category name in the heirarchy. Failover is included to use just the last category specified in the heirarchy, similar to the current implementation.
This implementation extends the category cache to include full path information upon cache generation. This info is used to index full category paths to their category IDs.
A similar approach might be used to affect the same problem within the page heirarchy.