From 55d721ffae404bb3a2f354858adb0d5e4bf5705c Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Thu, 6 Apr 2017 11:44:05 +0300 Subject: [PATCH] Optimized anagrams count computation --- .../PrecomputedPermutationsGenerator.cs | 37 +++++++++++-------- dotnet/WhiteRabbit/StringsProcessor.cs | 16 ++++++-- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/dotnet/WhiteRabbit/PrecomputedPermutationsGenerator.cs b/dotnet/WhiteRabbit/PrecomputedPermutationsGenerator.cs index 6424083..90757be 100644 --- a/dotnet/WhiteRabbit/PrecomputedPermutationsGenerator.cs +++ b/dotnet/WhiteRabbit/PrecomputedPermutationsGenerator.cs @@ -5,22 +5,13 @@ internal static class PrecomputedPermutationsGenerator { - private static PermutationsGenerator.Permutation[][] Permutations { get; } = new[] - { - GeneratePermutations(0), - GeneratePermutations(1), - GeneratePermutations(2), - GeneratePermutations(3), - GeneratePermutations(4), - GeneratePermutations(5), - GeneratePermutations(6), - GeneratePermutations(7), - }; - - public static PermutationsGenerator.Permutation[] HamiltonianPermutations(int n) - { - return Permutations[n]; - } + private static PermutationsGenerator.Permutation[][] Permutations { get; } = Enumerable.Range(0, 8).Select(GeneratePermutations).ToArray(); + + private static long[] PermutationsNumbers { get; } = GeneratePermutationsNumbers().Take(19).ToArray(); + + public static PermutationsGenerator.Permutation[] HamiltonianPermutations(int n) => Permutations[n]; + + public static long GetPermutationsNumber(int n) => PermutationsNumbers[n]; private static PermutationsGenerator.Permutation[] GeneratePermutations(int n) { @@ -32,5 +23,19 @@ return result.Concat(Enumerable.Repeat(result[0], Constants.PhrasesPerSet - (result.Length % Constants.PhrasesPerSet))).ToArray(); } + + private static IEnumerable GeneratePermutationsNumbers() + { + long result = 1; + yield return result; + + var i = 1; + while (true) + { + result *= i; + yield return result; + i++; + } + } } } diff --git a/dotnet/WhiteRabbit/StringsProcessor.cs b/dotnet/WhiteRabbit/StringsProcessor.cs index 5af2d39..bf40b4b 100644 --- a/dotnet/WhiteRabbit/StringsProcessor.cs +++ b/dotnet/WhiteRabbit/StringsProcessor.cs @@ -68,9 +68,8 @@ public long GetPhrasesCount() { return this.VectorsProcessor.GenerateSequences() - .Select(this.ConvertVectorsToWords) - .SelectMany(Flattener.Flatten) - .Sum(words => (long)PrecomputedPermutationsGenerator.HamiltonianPermutations(words.Length).Count()); + .Select(this.ConvertVectorsToWordsNumber) + .Sum(tuple => tuple.Item2 * PrecomputedPermutationsGenerator.GetPermutationsNumber(tuple.Item1)); } private byte[][][] ConvertVectorsToWords(int[] vectors) @@ -85,6 +84,17 @@ return words; } + private Tuple ConvertVectorsToWordsNumber(int[] vectors) + { + long result = 1; + for (var i = 0; i < vectors.Length; i++) + { + result *= this.WordsDictionary[vectors[i]].Length; + } + + return Tuple.Create(vectors.Length, result); + } + private IEnumerable ConvertWordsToPhrases(byte[][] words) { var permutations = PrecomputedPermutationsGenerator.HamiltonianPermutations(words.Length);