[Leetcode]388. Longest Absolute File Path
来源:互联网 发布:it猎头 编辑:程序博客网 时间:2024/06/06 10:43
Suppose we abstract our file system by a string in the following manner:
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
represents:
dir subdir1 subdir2 file.ext
The directory dir
contains an empty sub-directory subdir1
and a sub-directory subdir2
containing a file file.ext
.
The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
represents:
dir subdir1 file1.ext subsubdir1 subdir2 subsubdir2 file2.ext
The directory dir
contains two sub-directories subdir1
and subdir2
. subdir1
contains a file file1.ext
and an empty second-level sub-directory subsubdir1
. subdir2
contains a second-level sub-directory subsubdir2
containing a file file2.ext
.
We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is"dir/subdir2/subsubdir2/file2.ext"
, and its length is 32
(not including the double quotes).
Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0
.
Note:
- The name of a file contains at least a
.
and an extension. - The name of a directory or sub-directory will not contain a
.
.
Time complexity required: O(n)
where n
is the size of the input string.
Notice that a/aa/aaa/file1.txt
is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png
.
题解:这个给出的DFS版本。O(N)待续。
class Solution {private: unordered_map<string,vector<string>> edge; unordered_map<int,string> last_node; //每一层的最后一个节点public: int lengthLongestPath(string input) { int len=input.size(); if(len==0) return 0; last_node[0]=""; //定义一个根节点,为""。 int pos=0; while(pos<len) { int level=1; if(input[pos]=='\t') { while(input[pos]=='\t') { level++; pos++; } } int index=input.find("\n",pos); string name; if(index==-1) { name=input.substr(pos); pos=input.size(); } else { name=input.substr(pos,index-pos); pos=index+1; } edge[last_node[level-1]].push_back(name); last_node[level]=name; } int res=0; dfs("","",0,0,res); return res; } void dfs(string cur,string f,int dep,int dis,int& res) { if(cur.find(".")!=-1) { if(dis>res) { res=dis; } } for(int i=0;i<edge[cur].size();i++) { string nex=edge[cur][i]; if(nex==f) continue; if(dep>0) dfs(nex,cur,dep+1,dis+nex.size()+1,res); //加上每层的'/',第一层不加 else dfs(nex,cur,dep+1,dis+nex.size(),res); } }};
- LeetCode—388. Longest Absolute File Path
- LeetCode—388. Longest Absolute File Path
- leetcode -- 388. Longest Absolute File Path
- 388. [LeetCode]Longest Absolute File Path
- [Leetcode]388. Longest Absolute File Path
- 【LeetCode】 388. Longest Absolute File Path
- [LeetCode]388. Longest Absolute File Path
- LeetCode 388. Longest Absolute File Path
- [leetcode]388. Longest Absolute File Path
- leetcode:Longest Absolute File Path
- 【Leetcode】Longest Absolute File Path
- Leetcode Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 388. Longest Absolute File Path
- 61. Rotate List
- HDU 5538 House Building (水题)
- Toast 优化
- UVA 10905 Children's Game(贪心)
- CH 30 摆花 [动态规划] [矩阵快速幂优化]
- [Leetcode]388. Longest Absolute File Path
- Centreon+Nagios实战第四篇——监控端安装NDOUtils
- jsp分页
- 用多态实现模拟电脑-硬盘-U盘-MP3读写数据的功能
- View的事件分发机制
- Java hashMap
- 【NOIP2016提高A组五校联考1】挖金矿
- VIDEO视频OSD输出模块的理解
- jsp中JavaBean的使用