📌  相关文章
📜  返回主文件夹所需的最少操作数

📅  最后修改于: 2021-05-17 05:15:31             🧑  作者: Mango

给定一个字符串数组arr [],它表示在文件系统上执行的更改文件夹操作(Unix风格)。最初,文件系统在主文件夹中打开。该任务是查找以下三种类型的最小操作数以返回到主文件夹:

  • “ ../”:移动到当前文件夹的父文件夹。 (如果当前文件夹是主文件夹,则它将保留在同一文件夹中)。
  • “ ./”:保留在同一文件夹中。
  • “ F /”:移动到名为F的子文件夹。

例子:

方法:可以使用Stack解决该问题。请按照以下步骤解决问题:

  • 初始化一个变量,例如cntOp,以存储返回主文件夹所需的最少操作数。
  • 创建一个堆栈,说st来存储当前文件夹的路径
  • 遍历数组并检查以下条件:
    • 如果arr [i] ==“ ../”,则弹出堆栈的顶部元素。
    • 如果arr [i] ==“ F /”,则将arr [i]的值压入堆栈顶部。
  • 最后,打印留在堆栈中的元素计数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find minimum number of
// operations to go back to main folder
int minOperations(vector& arr,
                               int N)
{
    // Stores path of
    // the current folder
    stack st;
 
    for (int i = 0; i < N; i++) {
       
        // If stack is not empty and
        // the value of arr[i] is "../"
        if (arr[i] == "../" &&
                      !st.empty()) {
             
            // Pop top element of
            // the stack             
            st.pop();
        }
 
        // If the value of arr[i]
        // is like "F/"
        else if (arr[i] != "./") {
             
            // Push arr[i] on top element
            // of the stack              
            st.push(arr[i]);
        }
    }
 
    // Return count of elements left
    // into the stack
    return st.size();
}
 
// Driver Code
int main()
{
    vector arr
        = { "F1/", "F2/", "./",
          "F3/", "../", "F31/" };
    int N = arr.size();
    cout << minOperations(arr, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find minimum number of
// operations to go back to main folder
static int minOperations(String []arr, int N)
{
     
    // Stores path of
    // the current folder
    Stack st = new Stack<>();
 
    for(int i = 0; i < N; i++)
    {
         
        // If stack is not empty and
        // the value of arr[i] is "../"
        if (arr[i] == "../" && !st.empty())
        {
             
            // Pop top element of
            // the stack             
            st.pop();
        }
 
        // If the value of arr[i]
        // is like "F/"
        else if (arr[i] != "./")
        {
             
            // Push arr[i] on top element
            // of the stack              
            st.push(arr[i]);
        }
    }
 
    // Return count of elements left
    // into the stack
    return st.size();
}
 
// Driver Code
public static void main(String args[])
{
    String []arr = { "F1/", "F2/", "./",
                     "F3/", "../", "F31/" };
                      
    int N = arr.length;
     
    System.out.print(minOperations(arr, N));
}
}
 
// This code is contributed by ipg2016107


Python3
# Python3 program to implement
# the above appraoch
 
# Function to find minimum number of
# operations to go back to main folder
def minOperations(arr, N):
     
    # Stores path of
    # the current folder
    st = []
 
    for i in range(N):
         
        # If stack is not empty and
        # the value of arr[i] is "../"
        if (arr[i] == "../" and len(st) != 0):
             
            # Pop top element of
            # the stack
            st.pop(-1)
 
        # If the value of arr[i]
        # is like "F/"
        elif (arr[i] != "./"):
             
            # Push arr[i] on top element
            # of the stack
            st.append(arr[i])
 
    # Return count of elements left
    # into the stack
    return len(st)
 
# Driver code
if __name__ == '__main__':
     
    # Given array
    arr = [ "F1/", "F2/", "./",
            "F3/", "../", "F31/" ]
 
    # Size of the array
    N = len(arr)
 
    # Function Call
    print(minOperations(arr, N))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to find minimum
// number of operations to
// go back to main folder
static int minOperations(String []arr,
                         int N)
{   
  // Stores path of
  // the current folder
  Stack st =
        new Stack();
 
  for(int i = 0; i < N; i++)
  {
    // If stack is not empty
    // and the value of arr[i]
    // is "../"
    if (arr[i] == "../" &&
        st.Count != 0)
    {
      // Pop top element of
      // the stack             
      st.Pop();
    }
 
    // If the value of arr[i]
    // is like "F/"
    else if (arr[i] != "./")
    {
 
      // Push arr[i] on top
      // element of the stack              
      st.Push(arr[i]);
    }
  }
 
  // Return count of elements
  // left into the stack
  return st.Count;
}
 
// Driver Code
public static void Main(String []args)
{
  String []arr = {"F1/", "F2/", "./",
                  "F3/", "../", "F31/"};
  int N = arr.Length;
  Console.Write(minOperations(arr, N));
}
}
 
// This code is contributed by gauravrajput1


输出:
3









时间复杂度: O(N)
辅助空间: O(N)