Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Permutations
- {
- class Program
- {
- static void Main(string[] args)
- {
- var input = new char[int.Parse(Console.ReadLine())];
- for (int i = 0; i < input.Length; i++)
- {
- input[i] = char.Parse(Console.ReadLine());
- }
- Array.Sort(input);
- long counter = 0;
- do
- {
- if (Chek(input))
- {
- counter++;
- }
- } while (NextPermutation(input));
- Console.WriteLine(counter);
- }
- /// <summary>
- /// Transform array of chars to next permutation.
- /// Rearranges the elements into the next lexicographically greater permutation.
- /// </summary>
- /// <param name="array">The array of elements to be sorted</param>
- /// <returns>
- /// true if the function could rearrange the object as a lexicographically greater permutation.
- /// Otherwise, the function returns false to indicate that the arrangement is not greater than the previous, but the lowest possible (sorted in ascending order).
- /// </returns>
- private static bool NextPermutation(char[] array)
- {
- for (int index = array.Length - 2; index >= 0; index--)
- {
- if (array[index] < array[index + 1])
- {
- int swapWithIndex = array.Length - 1;
- while (array[index] >= array[swapWithIndex])
- {
- swapWithIndex--;
- }
- // Swap i-th and j-th elements
- var tmp = array[index];
- array[index] = array[swapWithIndex];
- array[swapWithIndex] = tmp;
- Array.Reverse(array, index + 1, array.Length - index - 1);
- return true;
- }
- }
- // No more permutations
- return false;
- }
- private static void Print(char[] arr)
- {
- for (int i = 0; i < arr.Length; i++)
- {
- Console.Write(arr[i]);
- }
- Console.WriteLine();
- }
- private static bool Chek(char[] arr)
- {
- for (int i = 1; i < arr.Length; i++)
- {
- if (arr[i] == arr[i - 1])
- {
- return false;
- }
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement