My original solution (whiteboard notes below) involved manually parsing the directory names by stepping over characters one by one looking for the slash character. This was somewhat fiddly and error prone and required some fixes after trying the test cases. I reimplemented using std::getline() and stringstream to do the tokenising, which proved simpler and less error prone. See gist source code for that implementation below.
Here’s the source code for that initial solution:
And here’s the source code for the simplified getline()/stringstream solution: