Advertisement
SHAMMY1

Untitled

Mar 4th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 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 Permutations
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var input = new char[int.Parse(Console.ReadLine())];
  14.  
  15.             for (int i = 0; i < input.Length; i++)
  16.             {
  17.                 input[i] = char.Parse(Console.ReadLine());
  18.             }
  19.             Array.Sort(input);
  20.             long counter = 0;
  21.  
  22.             do
  23.             {
  24.                 if (Chek(input))
  25.                 {
  26.                     counter++;
  27.                 }
  28.             } while (NextPermutation(input));
  29.  
  30.             Console.WriteLine(counter);
  31.         }
  32.  
  33.         /// <summary>
  34.         /// Transform array of chars to next permutation.
  35.         /// Rearranges the elements into the next lexicographically greater permutation.
  36.         /// </summary>
  37.         /// <param name="array">The array of elements to be sorted</param>
  38.         /// <returns>
  39.         /// true if the function could rearrange the object as a lexicographically greater permutation.
  40.         /// Otherwise, the function returns false to indicate that the arrangement is not greater than the previous, but      the lowest possible (sorted in ascending order).
  41.         /// </returns>
  42.         private static bool NextPermutation(char[] array)
  43.         {
  44.             for (int index = array.Length - 2; index >= 0; index--)
  45.             {
  46.                 if (array[index] < array[index + 1])
  47.                 {
  48.                     int swapWithIndex = array.Length - 1;
  49.                     while (array[index] >= array[swapWithIndex])
  50.                     {
  51.                         swapWithIndex--;
  52.                     }
  53.  
  54.                     // Swap i-th and j-th elements
  55.                     var tmp = array[index];
  56.                     array[index] = array[swapWithIndex];
  57.                     array[swapWithIndex] = tmp;
  58.  
  59.                     Array.Reverse(array, index + 1, array.Length - index - 1);
  60.                     return true;
  61.                 }
  62.             }
  63.  
  64.             // No more permutations
  65.             return false;
  66.         }
  67.  
  68.         private static void Print(char[] arr)
  69.         {
  70.             for (int i = 0; i < arr.Length; i++)
  71.             {
  72.                 Console.Write(arr[i]);
  73.             }
  74.             Console.WriteLine();
  75.         }
  76.  
  77.         private static bool Chek(char[] arr)
  78.         {
  79.             for (int i = 1; i < arr.Length; i++)
  80.             {
  81.                 if (arr[i] == arr[i - 1])
  82.                 {
  83.                     return false;
  84.                 }
  85.             }
  86.  
  87.             return true;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement