asd
This commit is contained in:
73
fordulo_3/Program/PermutationGenerator.cs
Normal file
73
fordulo_3/Program/PermutationGenerator.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Program
|
||||
{
|
||||
class PermutationGenerator
|
||||
{
|
||||
public Dictionary<string, List<int[]>> GroupedPermutations { get; private set; }
|
||||
private HashSet<string> uniquePermutations;
|
||||
|
||||
public PermutationGenerator()
|
||||
{
|
||||
GroupedPermutations = new Dictionary<string, List<int[]>>();
|
||||
uniquePermutations = new HashSet<string>();
|
||||
}
|
||||
|
||||
public void GeneratePermutations(int[] digits, int length)
|
||||
{
|
||||
GeneratePermutations(digits, new int[length], 0);
|
||||
}
|
||||
|
||||
private void GeneratePermutations(int[] digits, int[] current, int index)
|
||||
{
|
||||
if (index == current.Length)
|
||||
{
|
||||
if (current[0] != 0) // Avoid permutations starting with 0
|
||||
{
|
||||
AddPermutation(current);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < digits.Length; i++)
|
||||
{
|
||||
current[index] = digits[i];
|
||||
GeneratePermutations(digits, current, index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddPermutation(int[] permutation)
|
||||
{
|
||||
var key = string.Concat(permutation.OrderBy(x => x));
|
||||
var permutationString = string.Join("", permutation);
|
||||
|
||||
if (!uniquePermutations.Contains(permutationString))
|
||||
{
|
||||
uniquePermutations.Add(permutationString);
|
||||
|
||||
if (!GroupedPermutations.ContainsKey(key))
|
||||
{
|
||||
GroupedPermutations[key] = new List<int[]>();
|
||||
}
|
||||
GroupedPermutations[key].Add((int[])permutation.Clone());
|
||||
}
|
||||
}
|
||||
|
||||
public void PrintGroupedPermutations()
|
||||
{
|
||||
foreach (var group in GroupedPermutations)
|
||||
{
|
||||
Console.WriteLine($"Group: {group.Key}");
|
||||
foreach (var permutation in group.Value)
|
||||
{
|
||||
Console.WriteLine(string.Join("", permutation));
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user