Advertisement
Vladimir76

Magic exchangeable words

Feb 15th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 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 Magic_exchangeable_words
  8. {
  9.     class Program
  10.     {
  11.         static void AppendedNumber(StringBuilder sb, int[] wordIntCopy)
  12.         {
  13.             foreach (var numb in wordIntCopy)
  14.             {
  15.                 sb.Append(numb);
  16.             }
  17.         }
  18.  
  19.         static bool CompareOfStringForReplease(StringBuilder sbMinLength, StringBuilder sbMaxLength)
  20.         {
  21.  
  22.             string extract = sbMaxLength.ToString().Substring(0, sbMinLength.Length);
  23.             char[] endExtract = sbMaxLength.ToString().Substring(sbMinLength.Length).ToCharArray();
  24.             char[] sbCharArray = sbMinLength.ToString().ToCharArray();
  25.  
  26.             if (!sbMinLength.ToString().Equals(extract))
  27.             {
  28.                 return false;
  29.             }
  30.             else
  31.             {
  32.                 bool checkContains = false;
  33.                 for (int i = 0; i < endExtract.Length; i++)
  34.                 {
  35.                     if (!sbCharArray.Contains(endExtract[i]))
  36.                     {
  37.                         checkContains = true;
  38.                         break;
  39.                     }
  40.                 }
  41.                 if (checkContains)
  42.                 {
  43.                     return false;
  44.                 }
  45.                 else
  46.                 {
  47.                     return true;
  48.                 }
  49.             }
  50.         }
  51.  
  52.         static void Main(string[] args)
  53.         {
  54.             string[] arr = Console.ReadLine().Trim().Split();
  55.             StringBuilder sb1 = new StringBuilder();
  56.             StringBuilder sb2 = new StringBuilder();
  57.             bool check = false;
  58.             bool result = default(bool);
  59.             int savePosition = 0;
  60.  
  61.             for (int n = 0; n < arr.Length; n++)
  62.             {
  63.                 char[] word1 = arr[n].ToCharArray();
  64.                 int[] wordInt = word1.Select(x => Convert.ToInt32(x)).ToArray();
  65.                 int[] wordIntCopy = new int[wordInt.Length];
  66.  
  67.                 for (int i = 0; i < wordIntCopy.Length; i++)
  68.                 {
  69.                     wordIntCopy[i] = i + 1;
  70.                 }
  71.                 for (int i = 0; i < wordInt.Length - 1; i++)
  72.                 {
  73.                     for (int j = i + 1; j < wordInt.Length; j++)
  74.                     {
  75.                         if ((wordInt[i] == wordInt[j])&&(wordIntCopy[i]!=wordIntCopy[j]))
  76.                         {
  77.                             wordIntCopy[j] = i + 1;
  78.                         }
  79.                     }
  80.                     for (int m = i; m < wordIntCopy.Length-1; m++)
  81.                     {
  82.                         if (wordIntCopy[m]!=wordIntCopy[m+1])
  83.                         {
  84.                             savePosition = m;
  85.                             check = true;
  86.                             break;
  87.                         }
  88.                     }
  89.                     if (check)
  90.                     {
  91.                         i = savePosition;
  92.                     }
  93.                     check = false;
  94.                 }
  95.                 if (n==0)
  96.                 {
  97.                     AppendedNumber(sb1, wordIntCopy);
  98.                 }
  99.                 else
  100.                 {
  101.                     AppendedNumber(sb2, wordIntCopy);
  102.                 }
  103.             }
  104.             if (sb1.Length==sb2.Length)
  105.             {
  106.                 if (sb1.Equals(sb2))
  107.                 {
  108.                     Console.WriteLine(true.ToString().ToLower());
  109.                 }
  110.                 else
  111.                 {
  112.                     Console.WriteLine(false.ToString().ToLower());
  113.                 }
  114.             }
  115.             else
  116.             {
  117.                 if (sb1.Length < sb2.Length)
  118.                 {
  119.                     result = CompareOfStringForReplease(sb1, sb2);
  120.                 }
  121.                 else
  122.                 {
  123.                     result = CompareOfStringForReplease(sb2, sb1);
  124.                 }
  125.                 Console.WriteLine(result.ToString().ToLower());
  126.             }
  127.  
  128.         }      
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement