我怎样才能找到“最不常见的部分”?在同一逻辑驱动器上的路径之间?

发布于 2024-12-06 08:36:39 字数 754 浏览 0 评论 0原文

我的节目有几个路径可以观看,比如
C:\XML
C:\MyProg\Raw
C:\MyProg\Subset\MTSAT
C:\MyProg\Subset\GOESW
D:\数据集\复合
D:\数据集\全局
E:\Dataset\Mosaic

我想向我的 CFolderWatch 类实例添加 4 个路径,即 C:\XML、C:\MyProg、D:\Dataset 和 E:\Dataset,以便监视 insetad 的文件夹只要其“包含子目录”开关设置为 TRUE,即可使用上述所有 7 个路径。假设所有被监视的路径都已添加到向量容器中。

因此,我的问题是:如何在同一逻辑驱动器上的路径中找到“最不常见的部分”?先感谢您!

我的问题的详细解释: 1.我得到了一些用户定义的目录。 2.我想要这些目录被观看。 3. 在观看之前,我想做一些准备工作,例如,找到同一逻辑驱动器上的路径之间的公共部分,以避免可能向我的观看类添加太多路径。例如,逻辑盘C:上有3条路径,如下: C:\test\Data\R1、C:\test\Data\R2、C:\test\Data\R3,常用路径为“C:\test\Data”。因此,我们应该将“C:\test\Data”添加到监视模块中,而不是这三个路径。我的意思是这里的公共路径是它至少有一层目录。如果一条路径与其他路径没有共同路径,则仅返回不变。 4. 首先,算法应该处理不同的逻辑驱动器。也就是说,所有路径都必须根据各自的驱动器号进行分类。然后,在同一逻辑盘符上的传递路径中找到公共路径。

My program has several paths to be watched, such as
C:\XML
C:\MyProg\Raw
C:\MyProg\Subset\MTSAT
C:\MyProg\Subset\GOESW
D:\Dataset\Composite
D:\Dataset\Global
E:\Dataset\Mosaic

I want to add 4 paths, namely C:\XML, C:\MyProg, D:\Dataset and E:\Dataset, to my CFolderWatch class instance for the purpose of folder watching insetad of all 7 above-mentioned paths as long as its "Include Subdirectory" switch is set to TRUE. Suppose all paths of being watched have been added to a vector container.

Therefore, my question is: How can I find the "least common part" among the paths on the same logical drive? Thank you in advance!

Detailed explanation to my question:
1. I got some user-defined directories.
2. I want to these directories to be watched.
3. Before watching, I want to do some preparatory job, for example, find the common part among the path(s) on the same logical drive to avoid possibly adding so many paths to my watching class. For instance, if there are 3 paths on the logical drive C: as follows:
C:\test\Data\R1, C:\test\Data\R2, and C:\test\Data\R3, the common path is "C:\test\Data". So, we should add "C:\test\Data" to the watching module, not the three paths. What I mean the common path here is that it has at least one level of directory. If one path has no common path with the others, just returns unchanged.
4. First thing first, the algorithm should handle different logical drive(s). That is to say, all paths must be classified on the basis of their respective drive letter. Then, find the common path among the passed paths on the same logical drive letter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一生独一 2024-12-13 08:36:39

没有算法;你使用了不一致的逻辑。

仅考虑集合

  1. C:\XML
  2. C:\MyProg\Raw
  3. C:\MyProg\Subset\MTSAT
  4. C:\MyProg\Subset\GOESW

至少有 4 种不同的解决方案:

  1. C:\
  2. C:\XML 和 C:\MyProg \
  3. C:\XML 、 C:\MyProg\Raw 和 C:\MyProg\Subset
  4. C:\XML 、 C:\MyProg\Raw, C:\MyProg\Subset\MTSAT 和 C:\MyProg\Subset\GOESW

您无法解释为什么 2. 是正确的解决方案。可以编写一个算法,在给定解 N 作为输入的情况下找到解 N-1,因此您可以通过三个步骤从输入 (4) 到 (1)。但我们不明白为什么我们应该停在(2)。

There's no algorithm; you're using inconsistent logic.

Consider just the set

  1. C:\XML
  2. C:\MyProg\Raw
  3. C:\MyProg\Subset\MTSAT
  4. C:\MyProg\Subset\GOESW

There are at least 4 different solutions:

  1. C:\
  2. C:\XML and C:\MyProg\
  3. C:\XML , C:\MyProg\Raw and C:\MyProg\Subset
  4. C:\XML , C:\MyProg\Raw, C:\MyProg\Subset\MTSAT and C:\MyProg\Subset\GOESW

You fail to explain why 2. is the correct solution. It is possible to write an algorithm that finds solution N-1 given solution N as input, so you can get from the input (4) to (1) in three steps. But we don't understand why we should stop at (2).

横笛休吹塞上声 2024-12-13 08:36:39

两步算法:

  1. 按驱动器号和第一目录级别对目录进行分区。
  2. 对于每个分区,保留最长的前缀。

通过计算每个目录与其下一个同级目录共有的前缀字符数并保持最小值,可以轻松获得每个分区的最长前缀。例如,使用 mismatch

A two-step algorithm:

  1. Partition your directories by drive letter and first directory level.
  2. For each partition, keep the longest prefix.

The longest prefix per partition can be easily obtained by counting how many prefix characters each directory has in common with its next sibling, and keeping the minimum. Using mismatch, for example.

秋心╮凉 2024-12-13 08:36:39

正如 JB 所说,我认为这是一个两步解决方案,我已经在 C# 中尝试了一些基本编码。如果你想在java中使用它,我想你一定知道如何使用它:)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace So_Try1
{
      class Program
{
    public static int nthOccurrence(String str, char c, int n)
    {
        int pos = str.IndexOf(c, 0);
        while (n-- > 0 && pos != -1)
            pos = str.IndexOf(c, pos + 1);
        return pos;
    }

    static void Main(string[] args)
    {
        List<String> pathString = new List<string>();
        pathString.Add("C:\\XML");
    pathString.Add("C:\\MyProg\\Raw");
    pathString.Add("C:\\MyProg\\Subset\\MTSAT");
    pathString.Add("C:\\MyProg\\Subset\\GOESW");
        pathString.Add("D:\\Folder2\\Mosaic");
        pathString.Add("D:\\Folder2\\Mosaic\\SubFolder");
        pathString.Add("D:\\Dataset\\Composite");
        pathString.Add("D:\\Dataset\\Global");
        pathString.Add("F:\\Folder1\\Mosaic");
        pathString.Add("H:\\Folder2\\Mosaic");
        pathString.Add("D:\\Folder2\\Mosaic");
        pathString.Add("D:\\Folder2\\Mosaic\\SubFolder");
        pathString.Add("E:\\Dataset\\Mosaic"); 
        Dictionary<String, int> PathDict = new Dictionary<string,int>();

        foreach (String str in pathString)
        {
            int count = 0;
            foreach (char c in str)
                if (c == '\\') count++;
            while (count > 0)
            {
                int index = nthOccurrence(str, '\\', count);
                String tempPath;
                if (index < 0)
                {
                    //Console.WriteLine(str);
                    tempPath = str;
                }
                else
                {
                    //Console.WriteLine(str.Substring(0,index));
                    tempPath = str.Substring(0, index);
                }

                if (PathDict.ContainsKey(tempPath))
                {
                    PathDict[tempPath]++;
                }
                else
                {
                    foreach (var keys in PathDict.Keys)
                    {
                        if (tempPath.IndexOf(keys) > 0)
                        {
                            PathDict[keys]++;
                        }
                    }
                    PathDict.Add(tempPath, 1);
                }
                count--;
            }
        }
        foreach(var keyValue in PathDict){
            if(keyValue.Value > 1)
                Console.WriteLine(keyValue.Key);
            /*Console.WriteLine(keyValue.Key + " - " + keyValue.Value);*/
        }               
    }
}
}

这是一个非常简单的程序,假设你的路径在字符串中并检查重复项。如果路径数量较多,则必须采用更有效的解决方案。

As JB said I think this is a two step solution and I have tried some basic coding here in C#. If you want it in java I suppose you must be knowing how to use it :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace So_Try1
{
      class Program
{
    public static int nthOccurrence(String str, char c, int n)
    {
        int pos = str.IndexOf(c, 0);
        while (n-- > 0 && pos != -1)
            pos = str.IndexOf(c, pos + 1);
        return pos;
    }

    static void Main(string[] args)
    {
        List<String> pathString = new List<string>();
        pathString.Add("C:\\XML");
    pathString.Add("C:\\MyProg\\Raw");
    pathString.Add("C:\\MyProg\\Subset\\MTSAT");
    pathString.Add("C:\\MyProg\\Subset\\GOESW");
        pathString.Add("D:\\Folder2\\Mosaic");
        pathString.Add("D:\\Folder2\\Mosaic\\SubFolder");
        pathString.Add("D:\\Dataset\\Composite");
        pathString.Add("D:\\Dataset\\Global");
        pathString.Add("F:\\Folder1\\Mosaic");
        pathString.Add("H:\\Folder2\\Mosaic");
        pathString.Add("D:\\Folder2\\Mosaic");
        pathString.Add("D:\\Folder2\\Mosaic\\SubFolder");
        pathString.Add("E:\\Dataset\\Mosaic"); 
        Dictionary<String, int> PathDict = new Dictionary<string,int>();

        foreach (String str in pathString)
        {
            int count = 0;
            foreach (char c in str)
                if (c == '\\') count++;
            while (count > 0)
            {
                int index = nthOccurrence(str, '\\', count);
                String tempPath;
                if (index < 0)
                {
                    //Console.WriteLine(str);
                    tempPath = str;
                }
                else
                {
                    //Console.WriteLine(str.Substring(0,index));
                    tempPath = str.Substring(0, index);
                }

                if (PathDict.ContainsKey(tempPath))
                {
                    PathDict[tempPath]++;
                }
                else
                {
                    foreach (var keys in PathDict.Keys)
                    {
                        if (tempPath.IndexOf(keys) > 0)
                        {
                            PathDict[keys]++;
                        }
                    }
                    PathDict.Add(tempPath, 1);
                }
                count--;
            }
        }
        foreach(var keyValue in PathDict){
            if(keyValue.Value > 1)
                Console.WriteLine(keyValue.Key);
            /*Console.WriteLine(keyValue.Key + " - " + keyValue.Value);*/
        }               
    }
}
}

This is a very simple program that assumes that you have your paths in a string and checking for duplicates. If for a large no.of paths you must adopt a much more efficient solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文