Advertisement
b_gandurov

Untitled

May 27th, 2024 (edited)
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class Program
  6. {
  7.     static List<string> variations = new List<string>();
  8.     static int combLength;
  9.     static void Main()
  10.     {
  11.         combLength = int.Parse(Console.ReadLine());
  12.         string[] symbols = Console.ReadLine().Split(' ');
  13.         char X = symbols[0][0];
  14.         char Y = symbols[1][0];
  15.  
  16.         GenerateVariations("", X, Y);
  17.  
  18.         variations.Sort((x, y) => CustomSort(x, y));
  19.  
  20.         StringBuilder sb = new StringBuilder();
  21.         foreach (var variation in variations)
  22.         {
  23.             sb.AppendLine(variation);
  24.         }
  25.         Console.WriteLine(sb.ToString());
  26.     }
  27.  
  28.     private static int CustomSort(string x, string y)
  29.     {
  30.         for (int i = 0; i < x.Length; i++)
  31.         {
  32.             if (x[i] != y[i])
  33.             {
  34.                 return x[i].CompareTo(y[i]);
  35.             }
  36.         }
  37.         return x.CompareTo(y);
  38.     }
  39.  
  40.     static void GenerateVariations(string current, char X, char Y)
  41.     {
  42.         if (current.Length == combLength)
  43.         {
  44.             variations.Add(current);
  45.             return;
  46.         }
  47.         GenerateVariations(current + X, X, Y);
  48.         GenerateVariations(current + Y, X, Y);
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement