给定具有唯一字符和数字N的字符串str ,任务是使用Factoradic方法查找字符串的第N个字典排列。
例子:
Input: str = “abc”, N = 3
Output: bac
Explanation:
All possible permutations in sorted order: abc, acb, bac, bca, cab, cba
3rd permutation is bac
Input: str = “aba”, N = 2
Output: aba
Explanation:
All possible permutations in sorted order: aab, aba, baa
2nd permutation is aba
方法:这个想法是使用因式表征的概念。阶乘方法的主要概念是计算数字的序列。以下是使用阶乘方法查找第N个字典编排的步骤:
- 将N减1,因为此方法将排序顺序视为第0个排列。
- 将N除以1到字符串的长度,然后每次将余数存储在堆栈中,同时将N的值更新为N / i 。
- 每个步骤中计算出的余数是因数。因此,在计算了最终的因子表示之后,开始将元素追加到该位置上存在的结果字符串中。
- 每次迭代时从堆栈中删除该元素。
- 重复上述三个步骤,直到堆栈变空。
让我们通过一个例子来理解这种方法。假设字符串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
- 现在,将元素追加到堆栈中的结果字符串,并不断从堆栈中删除元素。在此,给定数字的分解式表示形式为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
输出:
acebd
注意:本文讨论了使用重复字符查找第N个排列的方法。