[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 subdir2subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1subdir2contains 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);        }    }};


0 0
原创粉丝点击