首页 > 代码库 > 388. Longest Absolute File Path
388. Longest Absolute File Path
题目如下:
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
.
这个题目总体是还是一个树类型的数据结构的题目,我在这里采用二叉树的结构完成的。
truct Node{
int Length;
int level;
int tl;
Node* p;//指向双亲结点
Node* b;//指向下一个兄弟结点
Node* fc;//指向第一个儿子结点
};
struct File{
Node *n;
File *next;
};
其中的Node用来存储整个文件系统的数据结构,包括所有的文件夹及文件。而File是一个链接所有文件的链表,用来简化查找longest file path。
具体实现代码如下:
int lengthLongestPath(string input) {
input += ‘\"‘;
int i=0;
string b = "";
Node *Tree = new Node;
Node *node;
Node *fnode = Tree;
Node *cnode = Tree;
File *file = new File;
file->next = NULL;
File *f = file;
File *nf;
int l = 1;
char c;
Tree->p = NULL;
Tree->fc = NULL;
Tree->Length = -1;
Tree->level = 0;
Tree->b = NULL;
while(i<input.size())
{
if((input.at(i)!=‘\n‘&&input.at(i)!=‘\"‘)&&input.at(i)!=‘\t‘)
{
c = input.at(i);
b += c;
++i;
}
else{
node = new Node;
node->b = NULL;
node->fc = NULL;
//cout<<cnode->level;
if(l == cnode->level + 1)
{
node->level = cnode->level + 1;
node->p = cnode;
if(fnode->fc == NULL)
{
fnode->fc = node;
}
}
else{
node->level = l;
//Node *fn = fnode;
//cout<<fn->level;
fnode = cnode;
int k = l - fnode->level;
while(k != 1)
{
fnode = fnode->p;
k = l - fnode->level;
}
node->p = fnode;
cnode = fnode->fc;
if(cnode==NULL)
{
fnode->fc = node;
}
else{
while(cnode->b!=NULL)
{
cnode = cnode->b;
}
cnode->b = node;
}
}
node->Length = b.size() + node->p->Length + 1;
cnode = node;
bool flag;
flag = false;
int j=b.size()-1;
for(;j>=0;--j)
{
if(b.at(j)==‘.‘)
{
flag = true;
break;
}
}
if(flag == true)
{
//cout<<node->Length<<endl;
nf = new File;
nf->n = node;
nf->next = NULL;
f->next = nf;
f = f->next;
}
if(i==input.size()-1)
break;
l=1;
i+=1;
while(input.at(i)==‘\t‘)
{
i+=1;
++l;
}
b = "";
//cout<<node->Length<<endl;
}
}
File* ff = file;
int maxnum = 0;
while(ff->next!=NULL)
{
if(ff->next->n->Length > maxnum)
maxnum = ff->next->n->Length;
ff = ff->next;
}
return maxnum;
}
};
388. Longest Absolute File Path