详见:https://leetcode.com/problems/longest-absolute-file-path/description/
C++:
class Solution { public: int lengthLongestPath(string input) { int res = 0, n = input.size(), level = 0; unordered_map<int, int> m {{0, 0}}; for (int i = 0; i < n; ++i) { int start = i; while (i < n && input[i] != ' ' && input[i] != ' ') { ++i; } if (i >= n || input[i] == ' ') { string t = input.substr(start, i - start); if (t.find('.') != string::npos) { res = max(res, m[level] + (int)t.size()); } else { ++level; m[level] = m[level - 1] + (int)t.size() + 1; } level = 0; } else { ++level; } } return res; } };
参考:https://www.cnblogs.com/grandyang/p/5806493.html