📜  最长公共目录路径的程序

📅  最后修改于: 2022-05-13 01:57:05.018000             🧑  作者: Mango

最长公共目录路径的程序

给定 n 个目录路径,我们需要编写一个程序来找到最长的公共目录路径。
例子:

Input : 3
"/home/User/Desktop/gfg/test"
"/home/User/Desktop/gfg/file"
"/home/User/Desktop/geeks/folders"

Output : Longest Common Path is /home/User/Desktop!

Input : 4
"/home/User/Desktop/gfg/test",
"/home/User/Desktop/gfg/file",
"/home/User/Desktop/gfg/folders",
"/home/User/Desktop/gfg/folders"


Output : Longest Common Path is /home/User/Desktop/gfg!

LongestCommonPath()函数中,使用了两个主要变量, “CommonCharacters”用于存储常见字符的长度, “CommonString”用于存储最长的路径字符串,以便我们获得最长的公共路径。
CommonCharacter 变量在每次迭代中根据输入路径中出现的公共路径的长度进行更新。
最后,我们得到迭代后公共路径字符串的长度。然后,我们通过子字符串获取我们的公共路径,直到分隔符'/'好像我们不这样做,然后我们得到路径中最长的公共字符串,这不应该是我们的结果
下面是上述方法的实现。

CPP
// CPP program for longest common path
#include 
using namespace std;
 
string LongestCommonPath(const vector& input_directory,
                                                char separator)
{
    vector::const_iterator iter;
 
    // stores the length of common characters
    // and initialized with the length of
    // first string .
    int CommonCharacters = input_directory[0].length();
 
    // stores the common string and initialized
    // with the first string .
    string CommonString = input_directory[0];
 
    for (iter = input_directory.begin() + 1;
        iter != input_directory.end(); iter++) {
 
        // finding the two pointers through the mismatch()
        // function which is 'mismatch at first string' and
        //  'mismatch at the other string '
        pair p =
                                mismatch(CommonString.begin(),
                                    CommonString.end(), iter->begin());
 
        // As the first mismatch string pointer points to the mismatch
        // present in the "CommonString" so "( p.first-CommonString.begin())"
        // determines the common characters in each iteration .
        if ((p.first - CommonString.begin()) < CommonCharacters) {
 
            // If the expression is smaller than commonCharacters
            // then we will update the commonCharacters because
            // this states that common path is smaller than the
            // string commonCharacters we have evaluated till .
            CommonCharacters = p.first - CommonString.begin();
        }
 
        // Updating CommonString variable
        // so that in this maximum length
        // string is stored .
        if (*iter > CommonString)
            CommonString = *iter;
    }
 
    // In 'found' variable we store the index of '/' in
    // the length of common characters in CommonString
    int found;
 
    for (int i = CommonCharacters; i >= 0; --i) {
        if (CommonString[i] == '/') {
            found = i;
            break;
        }
    }
 
    // The substring from index 0 to index of
    // separator is the longest common substring
    return CommonString.substr(0, found);
}
 
int main()
{
    int n = 4;
 
    string input_directory[4] = {
        "/home/User/Desktop/gfg/test",
        "/home/User/Desktop/gfg/file",
        "/home/User/Desktop/gfg/folders",
        "/home/User/Desktop/gfg/folders"
    };
 
    vector input(input_directory,
                        input_directory + n);
    cout << "Longest Common Path is "
        << LongestCommonPath(input, '/') << "!\n";
 
    return 0;
}


输出:
Longest Common Path is /home/User/Desktop/gfg!

参考 :
罗塞塔代码