Advertisement
stanevplamen

03.02.06.SecretLanguage

Jul 26th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2.  
  3. class SecretLanguage
  4. {
  5.     static void Main()
  6.     {
  7.         string inputCode = Console.ReadLine();
  8.         int numberOfWords = int.Parse(Console.ReadLine());
  9.         string inputLine = Console.ReadLine();
  10.  
  11.         string[] validWords = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  12.  
  13.         string[] sortedWords = new string[validWords.Length];
  14.         int[] sizeWords = new int[validWords.Length];
  15.  
  16.         for (int i = 0; i < validWords.Length; i++)
  17.         {
  18.             char[] tempArr = validWords[i].ToCharArray();
  19.             Array.Sort(tempArr);
  20.             sortedWords[i] = new string(tempArr);
  21.             sizeWords[i] = validWords[i].Length;
  22.         }
  23.  
  24.         int[] arrayOfResults = new int[inputCode.Length + 1];
  25.         arrayOfResults[0] = 0;
  26.         int start = 0;
  27.         isCounted = false;
  28.         for (int i = 1; i < arrayOfResults.Length; i++)
  29.         {
  30.             arrayOfResults[i] = int.MaxValue;
  31.             for (int j = 0; j < sizeWords.Length; j++)
  32.             {
  33.                 if (i - start == sizeWords[j])
  34.                 {
  35.                     int currentCost = FindCost(start, sizeWords[j], inputCode, validWords[j], sortedWords[j]);
  36.  
  37.                     if (arrayOfResults[i] > currentCost)
  38.                     {
  39.                         arrayOfResults[i] = currentCost;
  40.                     }
  41.                 }
  42.             }
  43.  
  44.             if (isCounted == true)
  45.             {
  46.                 isCounted = false;
  47.                 start = i;
  48.             }
  49.         }
  50.         if (arrayOfResults[arrayOfResults.Length - 1] == int.MaxValue)
  51.         {
  52.             Console.WriteLine(-1);
  53.         }
  54.         else
  55.         {
  56.             int sum = 0;
  57.             for (int i = 0; i < arrayOfResults.Length; i++)
  58.             {
  59.                 if (arrayOfResults[i] != int.MaxValue)
  60.                 {
  61.                     sum = sum + arrayOfResults[i];
  62.                 }
  63.             }
  64.             Console.WriteLine(sum);
  65.         }
  66.     }
  67.  
  68.     public static bool isCounted;
  69.  
  70.     private static int FindCost(int start, int finish, string inputCode, string validWord, string sortedWord)
  71.     {
  72.         string wordToCompare = inputCode.Substring(start, finish);
  73.         char[] tempArr = wordToCompare.ToCharArray();
  74.         Array.Sort(tempArr);
  75.         string wordToCompareSorted = new string(tempArr);
  76.         if (wordToCompareSorted == sortedWord)
  77.         {
  78.             isCounted = true;
  79.             int counter = 0;
  80.             for (int j = 0; j < wordToCompare.Length; j++)
  81.             {
  82.                 if (wordToCompare[j] != validWord[j])
  83.                 {
  84.                     counter++;
  85.                 }
  86.             }
  87.             return counter;
  88.         }
  89.         else
  90.         {
  91.             return int.MaxValue;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement