vencinachev

Permutations-E

Oct 31st, 2021 (edited)
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Perm
  8. {
  9.     class Program
  10.     {
  11.  
  12.         static List<string> perm = new List<string>();
  13.         static void swap(ref char a, ref char b)
  14.         {
  15.             char temp = a;
  16.             a = b;
  17.             b = temp;
  18.         }
  19.  
  20.         static void Permutate(char[] arr, int n)
  21.         {
  22.             if (n == arr.Length - 1)
  23.             {
  24.                 perm.Add(string.Join("", arr));
  25.                 return;
  26.             }
  27.             for (int i = n; i < arr.Length; i++)
  28.             {
  29.                 swap(ref arr[i], ref arr[n]);
  30.                 Permutate(arr, n + 1);
  31.                 swap(ref arr[i], ref arr[n]);
  32.             }
  33.         }
  34.         static void Permutate(string str)
  35.         {
  36.             Permutate(str.ToCharArray(), 0);
  37.         }
  38.         static void Main(string[] args)
  39.         {
  40.             string chars = Console.ReadLine().Replace(" ", "");
  41.             Permutate(chars);
  42.          
  43.             List<string> inputs = new List<string>();
  44.             while (true)
  45.             {
  46.                 string input = Console.ReadLine();
  47.                 if (input == "end") break;
  48.                 inputs.Add(input);
  49.             }
  50.  
  51.             bool flag = false;
  52.             inputs.Sort()
  53.             foreach (var item in inputs)
  54.             {
  55.                 if (perm.Contains(item))
  56.                 {
  57.                     flag = true;
  58.                     Console.WriteLine(string.Join(" ", item.ToCharArray()));
  59.                 }
  60.             }
  61.             if (!flag)
  62.             {
  63.                 Console.WriteLine("No permutations...");
  64.             }
  65.         }
  66.     }
  67. }
  68.  
Add Comment
Please, Sign In to add comment