各値には別個の意味があります。最初の値は順列の長さ、2番目の値は初期接頭辞の長さ、残りの整数は1つの整数ですすべての順列の接頭辞。固定接頭辞番号を持つ順列
配列の要素が{5,2,1,4}
の場合、5は置換配列の要素数です。 であり、2は配列順列の最初の2つの要素プレフィックスを構成する整数の長さです。 1,4は接頭辞整数、すなわち5要素置換組合せの長さ2であるので、欠落要素は2,3,5であり、1 &は以下のようにすべての置換にわたって共通の接頭辞である。
[14352] [14352] [14523] [14532]ここで、入力配列は{5,2,1,4}です
これを行うには?
私は1つの欠けている要素2,3 & 5の順列を取得するコードの下に持っていますが、私は
static void Main(string[] args)
{
int output;
int ip1;
ip1 = Convert.ToInt32(Console.ReadLine());
int ip2_size = 0;
ip2_size = Convert.ToInt32(Console.ReadLine());
int[] ip2 = new int[ip2_size];
int ip2_item;
for (int ip2_i = 0; ip2_i < ip2_size; ip2_i++)
{
ip2_item = Convert.ToInt32(Console.ReadLine());
ip2[ip2_i] = ip2_item;
}
output = correctResult(ip1, ip2);
Console.WriteLine(output);
}
static int correctResult(int n, int[] arr)
{
int permLength = 0;
int prefLength = 0;
int result = 0;
permLength = n;
prefLength = arr.Length;
int[] permArray = new int[permLength];
int len = 0;
var missingNum = Enumerable.Range(1,
permLength).Except(arr).ToArray<int>();
if (permLength < (missingNum.Length + len))
{
result = -1;
}
else
{
for (int i = 0; i < missingNum.Length; i++)
{
permArray[prefLength + i] = missingNum[i];
}
result = permute(missingNum, 0, missingNum.Length - 1);
}
return result;
}
static int permute(int[] arry, int i, int n)
{
int j;
if (i == n)
{
int s1, s2;
s1 = s2 = 0;
for (int a = 0; a < n - 1; a++)
{
for (int b = a + 1; b < n; b++)
{
if (arry[a] > arry[b])
{
s1++;
}
}
s2 = s2 + Math.Max(0, a + 1 - arry[a]);
}
int count = 0;
if (s1 == s2)
count++;
return count;
}
else
{
int count = 0;
for (j = i; j <= n; j++)
{
swap(ref arry[i], ref arry[j]);
count += permute(arry, i + 1, n);
swap(ref arry[i], ref arry[j]);
}
return count;
}
}
static void swap(ref int a, ref int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
あなたが持っているものこれまでに試しましたか? –
私の質問を更新しました – Yogesh