Быстрая перестановка → число → алгоритмы перестановочного отображения
У меня есть n элементов. Для примера, скажем, 7 элементов, 1234567. Я знаю, что есть 7!= 5040 возможных из этих 7 элементов.
Я хочу быстрый алгоритм, содержащий две функции:
f (число) отображает число от 0 до 5039 до уникальной перестановки и
f '(перестановка) отображает перестановку обратно на число, из которого оно было создано.
Меня не интересует соответствие между числом и перестановкой, поскольку каждая перестановка имеет свой собственный уникальный номер.
Так, например, у меня могут быть функции, где
f(0) = '1234567'
f'('1234567') = 0
Самый быстрый алгоритм, который приходит на ум, состоит в перечислении всех перестановок и создании таблицы поиска в обоих направлениях, так что, как только таблицы будут созданы, f (0) будет O (1) и f ('1234567') будет поиск строки. Однако это голод, особенно когда n становится большим.
Может ли кто-нибудь предложить другой алгоритм, который будет работать быстро и без недостатка памяти?
Ответы
Ответ 1
Чтобы описать перестановку n элементов, вы увидите, что для позиции, в которой находится первый элемент, у вас есть n возможностей, поэтому вы можете описать это числом от 0 до n-1. Для позиции, в которой находится следующий элемент, у вас есть n-1 оставшихся возможностей, поэтому вы можете описать это числом от 0 до n-2.
Et cetera, пока у вас не будет n чисел.
В качестве примера для n = 5 рассмотрим перестановку, которая приносит abcde
в caebd
.
-
a
, первый элемент заканчивается во второй позиции, поэтому мы присваиваем ему индекс 1.
-
b
заканчивается в четвертой позиции, которая будет индексом 3, но она остается третьей, поэтому мы присваиваем ей 2.
-
c
заканчивается в первом оставшемся положении, которое всегда 0.
-
d
заканчивается в последней оставшейся позиции, которая (из двух оставшихся позиций) 1.
-
e
заканчивается в единственной оставшейся позиции, индексируется в 0.
Итак, у нас есть индексная последовательность {1, 2, 0, 1, 0}.
Теперь вы знаете, что, например, в двоичном числе, "xyz" означает z + 2y + 4x. Для десятичного числа,
это z + 10y + 100x. Каждая цифра умножается на некоторый вес, и результаты суммируются. Очевидная закономерность в весе, конечно, состоит в том, что вес w = b ^ k, b - основание числа и k - индекс цифры. (Я всегда буду считать цифры справа и начинать с индекса 0 для самой правой цифры. Аналогично, когда я говорю о "первой" цифре, я имею в виду самый правый.)
Причина, по которой веса для цифр следуют этому шаблону, состоит в том, что наибольшее число, которое может быть представлено цифрами от 0 до k, должно быть ровно на 1 ниже самого низкого числа, которое может быть представлено только с использованием цифры k + 1. В двоичном коде 0111 должен быть меньше 1000. В десятичном значении 099999 должно быть меньше 100000.
Кодирование для переменной базы
Важным правилом является интервал между последующими числами, равным 1. Понимая это, мы можем представить нашу индексную последовательность с помощью номера переменной базы. Основанием для каждой цифры является количество различных возможностей для этой цифры. Для десятичной цифры у каждой цифры есть 10 возможностей, для нашей системы самая правая цифра имеет 1 возможность, а у самой левой - n возможностей. Но так как самая правая цифра (последнее число в нашей последовательности) всегда 0, мы ее не оставляем. Это означает, что мы остаемся с основаниями от 2 до n. В общем случае k-я цифра будет иметь базу b [k] = k + 2. Наибольшее значение, допустимое для цифры k, - h [k] = b [k] - 1 = k + 1.
В нашем правиле о весах w [k] цифр требуется, чтобы сумма h [i] * w [i], где я идет от я = 0 до я = k, равна 1 * w [k + 1]. Сформулированное рекуррентно, w [k + 1] = w [k] + h [k] * w [k] = w [k] * (h [k] + 1). Первый вес w [0] всегда должен быть 1. Начиная с этого момента мы имеем следующие значения:
k h[k] w[k]
0 1 1
1 2 2
2 3 6
3 4 24
... ... ...
n-1 n n!
(Общее соотношение w [k-1] = k! легко доказывается по индукции.)
Число, которое мы получаем от преобразования нашей последовательности, будет тогда суммой s [k] * w [k], где k работает от 0 до n-1. Здесь s [k] - элемент k'th (самый правый, начиная с 0) последовательности. В качестве примера возьмем наши {1, 2, 0, 1, 0}, причем самый правый элемент удаляется, как упоминалось ранее: {1, 2, 0, 1}. Наша сумма равна 1 * 1 + 0 * 2 + 2 * 6 + 1 * 24 = 37.
Заметим, что если мы возьмем максимальную позицию для каждого индекса, у нас будет {4, 3, 2, 1, 0}, и это преобразуется в 119. Поскольку веса нашей кодировки были выбраны так, что мы надеваем Пропустить любые числа, все числа от 0 до 119 действительны. Есть ровно 120 из них, это n! для n = 5 в нашем примере, точно количество различных перестановок. Таким образом, вы можете видеть, что наши закодированные числа полностью определяют все возможные перестановки.
Декодирование с переменной базой
Декодирование аналогично преобразованию в двоичный или десятичный. Общий алгоритм таков:
int number = 42;
int base = 2;
int[] bits = new int[n];
for (int k = 0; k < bits.Length; k++)
{
bits[k] = number % base;
number = number / base;
}
Для нашего номера переменной базы:
int n = 5;
int number = 37;
int[] sequence = new int[n - 1];
int base = 2;
for (int k = 0; k < sequence.Length; k++)
{
sequence[k] = number % base;
number = number / base;
base++; // b[k+1] = b[k] + 1
}
Это правильно декодирует наш 37 обратно до {1, 2, 0, 1} (sequence
будет {1, 0, 2, 1}
в этом примере кода, но что угодно... до тех пор, пока вы индексируете соответственно). Нам просто нужно добавить 0 в правый конец (помните, что последний элемент всегда имеет только одну возможность для своей новой позиции), чтобы вернуть нашу исходную последовательность {1, 2, 0, 1, 0}.
Перенос списка с использованием индексной последовательности
Вы можете использовать приведенный ниже алгоритм для перестановки списка в соответствии с определенной индексной последовательностью. Это, к сожалению, алгоритм O (n²).
int n = 5;
int[] sequence = new int[] { 1, 2, 0, 1, 0 };
char[] list = new char[] { 'a', 'b', 'c', 'd', 'e' };
char[] permuted = new char[n];
bool[] set = new bool[n];
for (int i = 0; i < n; i++)
{
int s = sequence[i];
int remainingPosition = 0;
int index;
// Find the s'th position in the permuted list that has not been set yet.
for (index = 0; index < n; index++)
{
if (!set[index])
{
if (remainingPosition == s)
break;
remainingPosition++;
}
}
permuted[index] = list[i];
set[index] = true;
}
Общее представление перестановок
Обычно вы не представляли бы перестановку как неинтуитивно, как мы это делали, а просто по абсолютной позиции каждого элемента после применения перестановки. Наш пример {1, 2, 0, 1, 0} для abcde
to caebd
обычно представлен {1, 3, 0, 4, 2}. Каждый индекс от 0 до 4 (или вообще от 0 до n-1) встречается ровно один раз в этом представлении.
Применение перестановки в этой форме легко:
int[] permutation = new int[] { 1, 3, 0, 4, 2 };
char[] list = new char[] { 'a', 'b', 'c', 'd', 'e' };
char[] permuted = new char[n];
for (int i = 0; i < n; i++)
{
permuted[permutation[i]] = list[i];
}
Инвертирование очень похоже:
for (int i = 0; i < n; i++)
{
list[i] = permuted[permutation[i]];
}
Преобразование из нашего представления в общее представление
Заметим, что если мы возьмем наш алгоритм для перестановки списка с использованием нашей индексной последовательности и применим его к перестановке тождеств {0, 1, 2,..., n-1}, получим обратную перестановку, представленную в общей форме, ( {2, 0, 4, 1, 3} в нашем примере).
Чтобы получить неинвертированное премутирование, применим алгоритм перестановки, который я только что показал:
int[] identity = new int[] { 0, 1, 2, 3, 4 };
int[] inverted = { 2, 0, 4, 1, 3 };
int[] normal = new int[n];
for (int i = 0; i < n; i++)
{
normal[identity[i]] = list[i];
}
Или вы можете просто применить перестановку напрямую, используя алгоритм обратной перестановки:
char[] list = new char[] { 'a', 'b', 'c', 'd', 'e' };
char[] permuted = new char[n];
int[] inverted = { 2, 0, 4, 1, 3 };
for (int i = 0; i < n; i++)
{
permuted[i] = list[inverted[i]];
}
Заметим, что все алгоритмы для работы с перестановками в общей форме - это O (n), а применение перестановки в нашей форме - O (n²). Если вам нужно применить перестановку несколько раз, сначала преобразуйте ее в общее представление.
Ответ 2
Я нашел алгоритм O (n), здесь краткое объяснение http://antoinecomeau.blogspot.ca/2014/07/mapping-between-permutations-and.html
public static int[] perm(int n, int k)
{
int i, ind, m=k;
int[] permuted = new int[n];
int[] elems = new int[n];
for(i=0;i<n;i++) elems[i]=i;
for(i=0;i<n;i++)
{
ind=m%(n-i);
m=m/(n-i);
permuted[i]=elems[ind];
elems[ind]=elems[n-i-1];
}
return permuted;
}
public static int inv(int[] perm)
{
int i, k=0, m=1;
int n=perm.length;
int[] pos = new int[n];
int[] elems = new int[n];
for(i=0;i<n;i++) {pos[i]=i; elems[i]=i;}
for(i=0;i<n-1;i++)
{
k+=m*pos[perm[i]];
m=m*(n-i);
pos[elems[n-i-1]]=pos[perm[i]];
elems[pos[perm[i]]]=elems[n-i-1];
}
return k;
}
Ответ 3
Сложность может быть сведена к n * log (n), см. раздел 10.1.1
( "Код Lehmer (таблица инверсии)", стр .232ff) fxtbook: http://www.jjj.de/fxt/#fxtbook
перейдите к разделу 10.1.1.1 ( "Вычисление с большими массивами" стр .235) для быстрого метода.
Код (GPLed, С++) находится на одной и той же веб-странице.
Ответ 4
Каждый элемент может находиться в одной из семи позиций. Чтобы описать позицию одного элемента, вам понадобятся три бита. Это означает, что вы можете сохранить позицию всех элементов в 32-битном значении. Это далеки от эффективности, поскольку это представление даже позволило бы всем элементам находиться в одном и том же положении, но я считаю, что бит-маскирование должно быть достаточно быстрым.
Однако, имея более 8 позиций, вам нужно что-то более изящное.
Ответ 5
Это происходит как встроенная функция в J:
A. 1 2 3 4 5 6 7
0
0 A. 1 2 3 4 5 6 7
1 2 3 4 5 6 7
?!7
5011
5011 A. 1 2 3 4 5 6 7
7 6 4 5 1 3 2
A. 7 6 4 5 1 3 2
5011
Ответ 6
Проблема решена. Тем не менее, я не уверен, что вам все еще нужно решение после этих лет. LOL, я просто присоединяюсь к этому сайту, поэтому...
Проверьте класс Java Permutation. Вы можете основываться на индексе, чтобы получить перестановку символов, или дать перестановку символов, затем получить индекс.
Вот мой класс прелюдии
/**
****************************************************************************************************************
* Copyright 2015 Fred Pang [email protected]
****************************************************************************************************************
* A complete list of Permutation base on an index.
* Algorithm is invented and implemented by Fred Pang [email protected]
* Created by Fred Pang on 18/11/2015.
****************************************************************************************************************
* LOL this is my first Java project. Therefore, my code is very much like C/C++. The coding itself is not
* very professional. but...
*
* This Permutation Class can be use to generate a complete list of all different permutation of a set of symbols.
* nPr will be n!/(n-r)!
* the user can input n = the number of items,
* r = the number of slots for the items,
* provided n >= r
* and a string of single character symbols
*
* the program will generate all possible permutation for the condition.
*
* Say if n = 5, r = 3, and the string is "12345", it will generate sll 60 different permutation of the set
* of 3 character strings.
*
* The algorithm I used is base on a bin slot.
* Just like a human or simply myself to generate a permutation.
*
* if there are 5 symbols to chose from, I'll have 5 bin slot to indicate which symbol is taken.
*
* Note that, once the Permutation object is initialized, or after the constructor is called, the permutation
* table and all entries are defined, including an index.
*
* eg. if pass in value is 5 chose 3, and say the symbol string is "12345"
* then all permutation table is logically defined (not physically to save memory).
* It will be a table as follows
* index output
* 0 123
* 1 124
* 2 125
* 3 132
* 4 134
* 5 135
* 6 143
* 7 145
* : :
* 58 542
* 59 543
*
* all you need to do is call the "String PermGetString(int iIndex)" or the "int[] PermGetIntArray(int iIndex)"
* function or method with an increasing iIndex, starting from 0 to getiMaxIndex() - 1. It will return the string
* or the integer array corresponding to the index.
*
* Also notice that in the input string is "12345" of position 01234, and the output is always in accenting order
* this is how the permutation is generated.
*
* ***************************************************************************************************************
* ==== W a r n i n g ====
* ***************************************************************************************************************
*
* There is very limited error checking in this class
*
* Especially the int PermGetIndex(int[] iInputArray) method
* if the input integer array contains invalid index, it WILL crash the system
*
* the other is the string of symbol pass in when the object is created, not sure what will happen if the
* string is invalid.
* ***************************************************************************************************************
*
*/
public class Permutation
{
private boolean bGoodToGo = false; // object status
private boolean bNoSymbol = true;
private BinSlot slot; // a bin slot of size n (input)
private int nTotal; // n number for permutation
private int rChose; // r position to chose
private String sSymbol; // character string for symbol of each choice
private String sOutStr;
private int iMaxIndex; // maximum index allowed in the Get index function
private int[] iOutPosition; // output array
private int[] iDivisorArray; // array to do calculation
public Permutation(int inCount, int irCount, String symbol)
{
if (inCount >= irCount)
{
// save all input values passed in
this.nTotal = inCount;
this.rChose = irCount;
this.sSymbol = symbol;
// some error checking
if (inCount < irCount || irCount <= 0)
return; // do nothing will not set the bGoodToGo flag
if (this.sSymbol.length() >= inCount)
{
bNoSymbol = false;
}
// allocate output storage
this.iOutPosition = new int[this.rChose];
// initialize the bin slot with the right size
this.slot = new BinSlot(this.nTotal);
// allocate and initialize divid array
this.iDivisorArray = new int[this.rChose];
// calculate default values base on n & r
this.iMaxIndex = CalPremFormula(this.nTotal, this.rChose);
int i;
int j = this.nTotal - 1;
int k = this.rChose - 1;
for (i = 0; i < this.rChose; i++)
{
this.iDivisorArray[i] = CalPremFormula(j--, k--);
}
bGoodToGo = true; // we are ready to go
}
}
public String PermGetString(int iIndex)
{
if (!this.bGoodToGo) return "Error: Object not initialized Correctly";
if (this.bNoSymbol) return "Error: Invalid symbol string";
if (!this.PermEvaluate(iIndex)) return "Invalid Index";
sOutStr = "";
// convert string back to String output
for (int i = 0; i < this.rChose; i++)
{
String sTempStr = this.sSymbol.substring(this.iOutPosition[i], iOutPosition[i] + 1);
this.sOutStr = this.sOutStr.concat(sTempStr);
}
return this.sOutStr;
}
public int[] PermGetIntArray(int iIndex)
{
if (!this.bGoodToGo) return null;
if (!this.PermEvaluate(iIndex)) return null ;
return this.iOutPosition;
}
// given an int array, and get the index back.
//
// ====== W A R N I N G ======
//
// there is no error check in the array that pass in
// if any invalid value in the input array, it can cause system crash or other unexpected result
//
// function pass in an int array generated by the PermGetIntArray() method
// then return the index value.
//
// this is the reverse of the PermGetIntArray()
//
public int PermGetIndex(int[] iInputArray)
{
if (!this.bGoodToGo) return -1;
return PermDoReverse(iInputArray);
}
public int getiMaxIndex() {
return iMaxIndex;
}
// function to evaluate nPr = n!/(n-r)!
public int CalPremFormula(int n, int r)
{
int j = n;
int k = 1;
for (int i = 0; i < r; i++, j--)
{
k *= j;
}
return k;
}
// PermEvaluate function (method) base on an index input, evaluate the correspond permuted symbol location
// then output it to the iOutPosition array.
//
// In the iOutPosition[], each array element corresponding to the symbol location in the input string symbol.
// from location 0 to length of string - 1.
private boolean PermEvaluate(int iIndex)
{
int iCurrentIndex;
int iCurrentRemainder;
int iCurrentValue = iIndex;
int iCurrentOutSlot;
int iLoopCount;
if (iIndex >= iMaxIndex)
return false;
this.slot.binReset(); // clear bin content
iLoopCount = 0;
do {
// evaluate the table position
iCurrentIndex = iCurrentValue / this.iDivisorArray[iLoopCount];
iCurrentRemainder = iCurrentValue % this.iDivisorArray[iLoopCount];
iCurrentOutSlot = this.slot.FindFreeBin(iCurrentIndex); // find an available slot
if (iCurrentOutSlot >= 0)
this.iOutPosition[iLoopCount] = iCurrentOutSlot;
else return false; // fail to find a slot, quit now
this.slot.setStatus(iCurrentOutSlot); // set the slot to be taken
iCurrentValue = iCurrentRemainder; // set new value for current value.
iLoopCount++; // increase counter
} while (iLoopCount < this.rChose);
// the output is ready in iOutPosition[]
return true;
}
//
// this function is doing the reverse of the permutation
// the input is a permutation and will find the correspond index value for that entry
// which is doing the opposit of the PermEvaluate() method
//
private int PermDoReverse(int[] iInputArray)
{
int iReturnValue = 0;
int iLoopIndex;
int iCurrentValue;
int iBinLocation;
this.slot.binReset(); // clear bin content
for (iLoopIndex = 0; iLoopIndex < this.rChose; iLoopIndex++)
{
iCurrentValue = iInputArray[iLoopIndex];
iBinLocation = this.slot.BinCountFree(iCurrentValue);
this.slot.setStatus(iCurrentValue); // set the slot to be taken
iReturnValue = iReturnValue + iBinLocation * this.iDivisorArray[iLoopIndex];
}
return iReturnValue;
}
/*******************************************************************************************************************
*******************************************************************************************************************
* Created by Fred on 18/11/2015. [email protected]
*
* *****************************************************************************************************************
*/
private static class BinSlot
{
private int iBinSize; // size of array
private short[] eStatus; // the status array must have length iBinSize
private BinSlot(int iBinSize)
{
this.iBinSize = iBinSize; // save bin size
this.eStatus = new short[iBinSize]; // llocate status array
}
// reset the bin content. no symbol is in use
private void binReset()
{
// reset the bin content
for (int i = 0; i < this.iBinSize; i++) this.eStatus[i] = 0;
}
// set the bin position as taken or the number is already used, cannot be use again.
private void setStatus(int iIndex) { this.eStatus[iIndex]= 1; }
//
// to search for the iIndex th unused symbol
// this is important to search through the iindex th symbol
// because this is how the table is setup. (or the remainder means)
// note: iIndex is the remainder of the calculation
//
// for example:
// in a 5 choose 3 permutation symbols "12345",
// the index 7 item (count starting from 0) element is "1 4 3"
// then comes the index 8, 8/12 result 0 -> 0th symbol in symbol string = '1'
// remainder 8. then 8/3 = 2, now we need to scan the Bin and skip 2 unused bins
// current the bin looks 0 1 2 3 4
// x o o o o x -> in use; o -> free only 0 is being used
// s s ^ skipped 2 bins (bin 1 and 2), we get to bin 3
// and bin 3 is the bin needed. Thus symbol "4" is pick
// in 8/3, there is a remainder 2 comes in this function as 2/1 = 2, now we have to pick the empty slot
// for the new 2.
// the bin now looks 0 1 2 3 4
// x 0 0 x 0 as bin 3 was used by the last value
// s s ^ we skip 2 free bins and the next free bin is bin 4
// therefor the symbol "5" at the symbol array is pick.
//
// Thus, for index 8 "1 4 5" is the symbols.
//
//
private int FindFreeBin(int iIndex)
{
int j = iIndex;
if (j < 0 || j > this.iBinSize) return -1; // invalid index
for (int i = 0; i < this.iBinSize; i++)
{
if (this.eStatus[i] == 0) // is it used
{
// found an empty slot
if (j == 0) // this is a free one we want?
return i; // yes, found and return it.
else // we have to skip this one
j--; // else, keep looking and count the skipped one
}
}
assert(true); // something is wrong
return -1; // fail to find the bin we wanted
}
//
// this function is to help the PermDoReverse() to find out what is the corresponding
// value during should be added to the index value.
//
// it is doing the opposite of int FindFreeBin(int iIndex) method. You need to know how this
// FindFreeBin() works before looking into this function.
//
private int BinCountFree(int iIndex)
{
int iRetVal = 0;
for (int i = iIndex; i > 0; i--)
{
if (this.eStatus[i-1] == 0) // it is free
{
iRetVal++;
}
}
return iRetVal;
}
}
}
// End of file - Permutation.java
и вот мой основной класс, показывающий, как использовать класс.
/*
* copyright 2015 Fred Pang
*
* This is the main test program for testing the Permutation Class I created.
* It can be use to demonstrate how to use the Permutation Class and its methods to generate a complete
* list of a permutation. It also support function to get back the index value as pass in a permutation.
*
* As you can see my Java is not very good. :)
* This is my 1st Java project I created. As I am a C/C++ programmer for years.
*
* I still have problem with the Scanner class and the System class.
* Note that there is only very limited error checking
*
*
*/
import java.util.Scanner;
public class Main
{
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args)
{
Permutation perm; // declear the object
String sOutString = "";
int nCount;
int rCount;
int iMaxIndex;
// Get user input
System.out.println("Enter n: ");
nCount = scanner.nextInt();
System.out.println("Enter r: ");
rCount = scanner.nextInt();
System.out.println("Enter Symbol: ");
sOutString = scanner.next();
if (sOutString.length() < rCount)
{
System.out.println("String too short, default to numbers");
sOutString = "";
}
// create object with user requirement
perm = new Permutation(nCount, rCount, sOutString);
// and print the maximum count
iMaxIndex = perm.getiMaxIndex();
System.out.println("Max count is:" + iMaxIndex);
if (!sOutString.isEmpty())
{
for (int i = 0; i < iMaxIndex; i++)
{ // print out the return permutation symbol string
System.out.println(i + " " + perm.PermGetString(i));
}
}
else
{
for (int i = 0; i < iMaxIndex; i++)
{
System.out.print(i + " ->");
// Get the permutation array
int[] iTemp = perm.PermGetIntArray(i);
// print out the permutation
for (int j = 0; j < rCount; j++)
{
System.out.print(' ');
System.out.print(iTemp[j]);
}
// to verify my PermGetIndex() works. :)
if (perm.PermGetIndex(iTemp)== i)
{
System.out.println(" .");
}
else
{ // oops something is wrong :(
System.out.println(" ***************** F A I L E D *************************");
assert(true);
break;
}
}
}
}
}
//
// End of file - Main.java
Получайте удовольствие.:)
Ответ 7
Вы можете кодировать перестановки с использованием рекурсивного алгоритма. Если N-перестановка (некоторый порядок чисел {0,..., N-1}) имеет следующий вид: {x,...}, то кодировать ее как x + N * кодирование (N-1) -пермутация, представленная "..." на числа {0, N-1} - {x}. Звучит как глоток, вот какой код:
// perm[0]..perm[n-1] must contain the numbers in {0,..,n-1} in any order.
int permToNumber(int *perm, int n) {
// base case
if (n == 1) return 0;
// fix up perm[1]..perm[n-1] to be a permutation on {0,..,n-2}.
for (int i = 1; i < n; i++) {
if (perm[i] > perm[0]) perm[i]--;
}
// recursively compute
return perm[0] + n * permToNumber(perm + 1, n - 1);
}
// number must be >=0, < n!
void numberToPerm(int number, int *perm, int n) {
if (n == 1) {
perm[0] = 0;
return;
}
perm[0] = number % n;
numberToPerm(number / n, perm + 1, n - 1);
// fix up perm[1] .. perm[n-1]
for (int i = 1; i < n; i++) {
if (perm[i] >= perm[0]) perm[i]++;
}
}
Этот алгоритм O (n ^ 2). Бонусные очки, если у кого есть алгоритм O (n).
Ответ 8
Какой интересный вопрос!
Если все ваши элементы являются числами, вы можете рассмотреть возможность их преобразования из строк в реальные числа. Затем вы сможете отсортировать все перестановки, поместив их в порядок и поместите их в массив. После этого вы были бы открыты для любого из различных алгоритмов поиска.
Ответ 9
Я был поспешным в своем предыдущем ответе (удален), однако у меня есть фактический ответ. Это обеспечивается аналогичной концепцией factoradic и связано с перестановками (мой ответ, связанный с комбинациями, я извиняюсь за эту путаницу). Я ненавижу просто публиковать ссылки в Википедии, но я пишу, что я некоторое время назад непонятен по какой-то причине. Таким образом, я могу расширить это позже, если потребуется.
Ответ 10
Об этом написана книга. Извините, но я не помню его имени (вы найдете это, скорее всего, из Википедии).
но в любом случае я написал реализацию python этой системы перечисления: http://kks.cabal.fi/Kombinaattori
Некоторые из них на финском языке, но просто скопируйте переменные кода и имени...
Ответ 11
Смежным вопросом является вычисление обратной перестановки, перестановки, которая восстановит переставленные векторы в исходный порядок, когда известен только массив перестановок. Вот код O (n) (в PHP):
// Compute the inverse of a permutation
function GetInvPerm($Perm)
{
$n=count($Perm);
$InvPerm=[];
for ($i=0; $i<$n; ++$i)
$InvPerm[$Perm[$i]]=$i;
return $InvPerm;
} // GetInvPerm
Дэвид Спектор Springtime Software
Ответ 12
У меня был именно этот вопрос, и я подумал, что предоставлю свое решение на Python. Это О (п ^ 2).
import copy
def permute(string, num):
''' generates a permutation '''
def build_s(factoradic): # Build string from factoradic in list form
string0 = copy.copy(string)
n = []
for i in range(len(factoradic)):
n.append(string0[factoradic[i]])
del string0[factoradic[i]]
return n
f = len(string)
factoradic = []
while(f != 0): # Generate factoradic number list
factoradic.append(num % f)
num = (num - factoradic[-1])//f
f -= 1
return build_s(factoradic)
s = set()
# Print 120 permutations of this string
for i in range(120):
m = permute(list('abcde'), i)
s.add(''.join(m))
print(len(s)) # Check that we have 120 unique permutations
Это довольно прямо; после генерации факторического представления числа я просто выбираю и удаляю символы из строки. Удаление из строки - вот почему это решение O (n ^ 2).
Антуан раствор лучше для производительности.
Ответ 13
[email protected] Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx