📌  相关文章
📜  使用 Factoradic 方法查找字符串的第 N 个字典排列

📅  最后修改于: 2021-09-06 17:46:03             🧑  作者: Mango

给定具有唯一字符和数字N 的字符串str ,任务是使用 Factoradic 方法找到字符串的第 N 个字典排列。
例子:

方法:这个想法是使用因子表示的概念。因子法的主要概念是计算一个数的序列。以下是使用 factoradic 方法找到第 N 个字典排列的步骤:

  1. N1,因为此方法将排序顺序视为第 0 次排列。
  2. 将 N 除以 1 为字符串的长度,每次将余数存储在堆栈中,同时将 N 的值更新为N/i
  3. 每一步计算出的余数就是阶乘数。因此,在计算出最终的因子表示之后,开始在结果字符串附加该位置上的元素。
  4. 在每次迭代时从堆栈中删除元素。
  5. 重复以上三步,直到栈为空。

让我们通过一个例子来理解这个方法。让字符串str 为“abcde” ,N 为11 。然后:

  • 最初,从 N 中减去 1。
N = 11 - 1
N = 10
  • 现在,在每次迭代中,将 N 与 i 相除,其中 i 的范围从 1 到长度,并将余数存储在堆栈中:
divide       Remainder    Quotient     Factoradic
10%1           0            10              0
10%2           0             5             00
5%3            2             1            200
2%4            1             0           1200
2%5            0             0          01200  
  • 现在,将元素追加到堆栈中的结果字符串,并不断从堆栈中删除元素。这里,给定数字的 Factoradic 表示是 01200。因此:
[0]=a   <- Selected
[1]=b
[2]=d 
[3]=e
[4]=f

result = a

[0]=b
[1]=c <- Selected
[2]=d
[3]=e

result= ac

[0]=b
[1]=d
[2]=e <-Selected

result= ace

[0]=b <- Selected
[1]=d

result= aceb

[0]=d <-selected

result =acebd
  • 因此,字符串的第 11 个排列是“acebd”

下面是上述方法的实现:

C++
// C++ program to find the N-th lexicographic
// permutation of string using Factroid method
#include 
using namespace std;
 
// Function to calculate nth permutation of string
void string_permutation(long long int n, string str)
{
    // Creating an empty stack
    stack s;
    string result;
 
    // Subtracting 1 from N because the
    // permutations start from 0 in
    // factroid method
    n = n - 1;
 
    // Loop to generate the factroid
    // of the sequence
    for (int i = 1; i < str.size() + 1; i++) {
        s.push(n % i);
        n = n / i;
    }
 
    // Loop to generate nth permutation
    for (int i = 0; i < str.size(); i++) {
        int a = s.top();
        result += str[a];
        int j;
 
        // Remove 1-element in each cycle
        for (j = a; j < str.length(); j++)
            str[j] = str[j + 1];
        str[j + 1] = '\0';
        s.pop();
    }
 
    // Final answer
    cout << result << endl;
}
 
// Driver code
int main()
{
    string str = "abcde";
    long long int n = 11;
 
    string_permutation(n, str);
    return 0;
}


Java
// Java program to find the N-th lexicographic
// permutation of String using Factroid method
import java.util.*;
 
class GFG{
 
// Function to calculate nth permutation of String
static void String_permutation(int n, String str)
{
     
    // Creating an empty stack
    Stack s = new Stack();
    String result = "";
 
    // Subtracting 1 from N because the
    // permutations start from 0 in
    // factroid method
    n = n - 1;
 
    // Loop to generate the factroid
    // of the sequence
    for(int i = 1; i < str.length() + 1; i++)
    {
       s.add(n % i);
       n = n / i;
    }
 
    // Loop to generate nth permutation
    for(int i = 0; i < str.length(); i++)
    {
       int a = s.peek();
       result += str.charAt(a);
       int j;
        
       // Remove 1-element in each cycle
       for(j = a; j < str.length() - 1; j++)
          str = str.substring(0, j) +
                str.charAt(j + 1) +
                str.substring(j + 1);
                 
       str = str.substring(0, j + 1);
       s.pop();
    }
 
    // Final answer
    System.out.print(result + "\n");
}
 
// Driver code
public static void main(String[] args)
{
    String str = "abcde";
    int n = 11;
 
    String_permutation(n, str);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to find
# the N-th lexicographic
# permutation of string
# using Factroid method
 
# Function to calculate
# nth permutation of string
def string_permutation(n, str):
   
    # Creating an empty list
    s = []
    result = ""
    str = list(str)
 
    # Subtracting 1 from N
    # because the permutations
    # start from 0 in factroid method
    n = n - 1
 
    # Loop to generate the
    # factroid of the sequence
    for i in range(1, len(str) + 1):
        s.append(n % i)
        n = int(n / i)
 
    # Loop to generate
    # nth permutation
    for i in range(len(str)):
        a = s[-1]
        result += str[a]
         
        # Remove 1-element in
        # each cycle
        for j in range(a, len(str) - 1):
            str[j] = str[j + 1]
             
        str[j + 1] = '\0'
        s.pop()
         
    # Final answer
    print(result)
 
# Driver code
str = "abcde"
n = 11
string_permutation(n, str)
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program to find the N-th lexicographic
// permutation of String using Factroid method
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate nth permutation of String
static void String_permutation(int n, String str)
{
     
    // Creating an empty stack
    Stack s = new Stack();
    String result = "";
 
    // Subtracting 1 from N because the
    // permutations start from 0 in
    // factroid method
    n = n - 1;
 
    // Loop to generate the factroid
    // of the sequence
    for(int i = 1; i < str.Length + 1; i++)
    {
       s.Push(n % i);
       n = n / i;
    }
 
    // Loop to generate nth permutation
    for(int i = 0; i < str.Length; i++)
    {
       int a = s.Peek();
       result += str[a];
       int j;
        
       // Remove 1-element in each cycle
       for(j = a; j < str.Length - 1; j++)
       {
          str = str.Substring(0, j) +
                str[j + 1] +
                str.Substring(j + 1);
       }
       str = str.Substring(0, j + 1);
       s.Pop();
    }
 
    // Final answer
    Console.Write(result + "\n");
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "abcde";
    int n = 11;
 
    String_permutation(n, str);
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
acebd

注意:本文讨论了一种找到具有重复字符的N排列的方法。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live