在文件和目录列表中找到公用的父路径 [英] Find common parent-path in list of files and directories

查看:107
本文介绍了在文件和目录列表中找到公用的父路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了文件和目录List<string> pathes的列表.现在,我想计算每个路径彼此共享的最深的公共分支.

I got a list of files and directories List<string> pathes. Now I'd like to calculate the deepest common branch every path is sharing with each other.

我们可以假设它们都具有相同的路径,但是一开始这是未知的.

We can assume that they all share a common path, but this is unknown in the beginning.

假设我有以下三个条目:

Let's say I have the following three entries:

  • C:/Hello/World/This/Is/An/Example/Bla.cs
  • C:/Hello/World/This/Is/Not/An/Example/
  • C:/你好/地球/Bla/Bla/Bla

这应该得到结果:C:/Hello/,因为地球正在破坏子目录的这个链".

This should get the result: C:/Hello/ as Earth is breaking this "chain" of subdirectories.

第二个例子:

  • C:/Hello/World/This/Is/An/Example/Bla.cs
  • C:/Hello/World/This/Is/Not/An/Example/

-> C:/Hello/World/This/Is/

-> C:/Hello/World/This/Is/

您将如何进行?我尝试使用string.split(@"/")并从第一个字符串开始,然后检查此数组的每个部分是否包含在其他字符串中.但是,这在我迭代(list_of_entries)^ list_of_entries时将是一个非常昂贵的调用.有没有更好的解决方案?

How would you proceed? I tried to use string.split(@"/") and start with the first string and check if every part of this array is contained in the other strings. However, this would be a very expensive call as I'm iterating (list_of_entries)^list_of_entries. Is there any better solution available?

我当前的尝试将类似于以下内容(C#+ LINQ):

My current attempt would be something like the following (C# + LINQ):

    public string CalculateCommonPath(IEnumerable<string> paths)
    {
        int minSlash = int.MaxValue;
        string minPath = null;
        foreach (var path in paths)
        {
            int splits = path.Split('\\').Count();
            if (minSlash > splits)
            {
                minSlash = splits;
                minPath = path;
            }
        }

        if (minPath != null)
        {
            string[] splits = minPath.Split('\\');
            for (int i = 0; i < minSlash; i++)
            {
                if (paths.Any(x => !x.StartsWith(splits[i])))
                {
                    return i >= 0 ? splits.Take(i).ToString() : "";
                }
            }
        }
        return minPath;
    }

推荐答案

获得最长公共前缀的函数可能看起来像这样:

A function to get the longest common prefix may look like this:

public static string GetLongestCommonPrefix(string[] s)
{
    int k = s[0].Length;
    for (int i = 1; i < s.Length; i++)
    {
        k = Math.Min(k, s[i].Length);
        for (int j = 0; j < k; j++)
            if (s[i][j] != s[0][j])
            {
                k = j;
                break;
            }
    }
    return s[0].Substring(0, k);
}

然后,您可能需要在右手边剪掉前缀.例如.我们要返回c:/dir而不是c:/dir/file

Then you may need to cut the prefix on the right hand. E.g. we want to return c:/dir instead of c:/dir/file for

c:/dir/file1
c:/dir/file2

您可能还需要在处理之前对路径进行规范化.请参阅在C#中标准化目录名称.

You also may want to normalize the paths before processing. See Normalize directory names in C#.

这篇关于在文件和目录列表中找到公用的父路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆