Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. // using System.Collections.Frozen;
  2. using System.Diagnostics;
  3. using System.Text;
  4.  
  5. internal class Program
  6. {
  7. static char[] templateWord = "камарилья".ToCharArray();
  8. static HashSet<string> RussianNouns;
  9.  
  10.  
  11. private static void Main(string[] args)
  12. {
  13. Console.WriteLine("Hello, World!");
  14.  
  15. RussianNouns = File.ReadAllLines("russian_nouns.txt").ToHashSet();
  16. var sw = Stopwatch.StartNew();
  17. List<string> generated = GetWords();
  18. Console.WriteLine(string.Join(',', generated));
  19.  
  20. Console.WriteLine($"finish! {sw.ElapsedMilliseconds}");
  21.  
  22. }
  23.  
  24. private static List<string> GetWords()
  25. {
  26. int n = templateWord.Length;
  27. int m = 6;
  28. int[] arr = new int[n];
  29.  
  30. for (int i = 0; i < n; i++)
  31. arr[i] = i + 1;
  32.  
  33. //Print(a, m);
  34. List<string> generated = new(1000);
  35.  
  36. while (NextSet(arr, n, m))
  37. {
  38. // var ww = BuildWord(arr, m);
  39. int[] arrTarget = new int[m];
  40.  
  41. Array.Copy(arr, arrTarget, m);
  42. //Print(arrTarget);
  43. var genWords = GetPermutations(arrTarget, m).Select(x => BuildWord2(x.ToArray()));
  44. foreach (var ww in genWords)
  45. {
  46. if (RussianNouns.Contains(ww) && !generated.Contains(ww))
  47. generated.Add(ww);
  48. }
  49. }
  50.  
  51. return generated;
  52. }
  53.  
  54. static bool NextSet(int[] arry, int n, int m)
  55. {
  56. int k = m;
  57. for (int i = k - 1; i >= 0; --i)
  58. if (arry[i] < n - k + i + 1)
  59. {
  60. ++arry[i];
  61. for (int j = i + 1; j < k; ++j)
  62. arry[j] = arry[j - 1] + 1;
  63. return true;
  64. }
  65. return false;
  66. }
  67.  
  68. static IEnumerable<IEnumerable<T>>
  69. GetPermutations<T>(IEnumerable<T> list, int length)
  70. {
  71. if (length == 1) return list.Select(t => new T[] { t });
  72.  
  73. return GetPermutations(list, length - 1)
  74. .SelectMany(t => list.Where(e => !t.Contains(e)),
  75. (t1, t2) => t1.Concat(new T[] { t2 }));
  76. }
  77.  
  78. static void Print(int[] a) => Console.WriteLine( $"[{string.Join(',',a)}]");
  79.  
  80. static string BuildWord(int[] a, int k) =>
  81. new string(a[0..k].Select(x => templateWord[x - 1]).ToArray());
  82.  
  83. static string BuildWord2(int[] a)
  84. {
  85. int k = a.Length;
  86. var res = new StringBuilder(k);
  87. for (int i = 0; i < k; i++)
  88. {
  89. res.Append(templateWord[a[i] - 1]);
  90. }
  91. return res.ToString();
  92. }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement