Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // using System.Collections.Frozen;
- using System.Diagnostics;
- using System.Text;
- internal class Program
- {
- static char[] templateWord = "камарилья".ToCharArray();
- static HashSet<string> RussianNouns;
- private static void Main(string[] args)
- {
- Console.WriteLine("Hello, World!");
- RussianNouns = File.ReadAllLines("russian_nouns.txt").ToHashSet();
- var sw = Stopwatch.StartNew();
- List<string> generated = GetWords();
- Console.WriteLine(string.Join(',', generated));
- Console.WriteLine($"finish! {sw.ElapsedMilliseconds}");
- }
- private static List<string> GetWords()
- {
- int n = templateWord.Length;
- int m = 6;
- int[] arr = new int[n];
- for (int i = 0; i < n; i++)
- arr[i] = i + 1;
- //Print(a, m);
- List<string> generated = new(1000);
- while (NextSet(arr, n, m))
- {
- // var ww = BuildWord(arr, m);
- int[] arrTarget = new int[m];
- Array.Copy(arr, arrTarget, m);
- //Print(arrTarget);
- var genWords = GetPermutations(arrTarget, m).Select(x => BuildWord2(x.ToArray()));
- foreach (var ww in genWords)
- {
- if (RussianNouns.Contains(ww) && !generated.Contains(ww))
- generated.Add(ww);
- }
- }
- return generated;
- }
- static bool NextSet(int[] arry, int n, int m)
- {
- int k = m;
- for (int i = k - 1; i >= 0; --i)
- if (arry[i] < n - k + i + 1)
- {
- ++arry[i];
- for (int j = i + 1; j < k; ++j)
- arry[j] = arry[j - 1] + 1;
- return true;
- }
- return false;
- }
- static IEnumerable<IEnumerable<T>>
- GetPermutations<T>(IEnumerable<T> list, int length)
- {
- if (length == 1) return list.Select(t => new T[] { t });
- return GetPermutations(list, length - 1)
- .SelectMany(t => list.Where(e => !t.Contains(e)),
- (t1, t2) => t1.Concat(new T[] { t2 }));
- }
- static void Print(int[] a) => Console.WriteLine( $"[{string.Join(',',a)}]");
- static string BuildWord(int[] a, int k) =>
- new string(a[0..k].Select(x => templateWord[x - 1]).ToArray());
- static string BuildWord2(int[] a)
- {
- int k = a.Length;
- var res = new StringBuilder(k);
- for (int i = 0; i < k; i++)
- {
- res.Append(templateWord[a[i] - 1]);
- }
- return res.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement