Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class SecretLanguage
- {
- static void Main()
- {
- string inputCode = Console.ReadLine();
- int numberOfWords = int.Parse(Console.ReadLine());
- string inputLine = Console.ReadLine();
- string[] validWords = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- string[] sortedWords = new string[validWords.Length];
- int[] sizeWords = new int[validWords.Length];
- for (int i = 0; i < validWords.Length; i++)
- {
- char[] tempArr = validWords[i].ToCharArray();
- Array.Sort(tempArr);
- sortedWords[i] = new string(tempArr);
- sizeWords[i] = validWords[i].Length;
- }
- int[] arrayOfResults = new int[inputCode.Length + 1];
- arrayOfResults[0] = 0;
- int start = 0;
- isCounted = false;
- for (int i = 1; i < arrayOfResults.Length; i++)
- {
- arrayOfResults[i] = int.MaxValue;
- for (int j = 0; j < sizeWords.Length; j++)
- {
- if (i - start == sizeWords[j])
- {
- int currentCost = FindCost(start, sizeWords[j], inputCode, validWords[j], sortedWords[j]);
- if (arrayOfResults[i] > currentCost)
- {
- arrayOfResults[i] = currentCost;
- }
- }
- }
- if (isCounted == true)
- {
- isCounted = false;
- start = i;
- }
- }
- if (arrayOfResults[arrayOfResults.Length - 1] == int.MaxValue)
- {
- Console.WriteLine(-1);
- }
- else
- {
- int sum = 0;
- for (int i = 0; i < arrayOfResults.Length; i++)
- {
- if (arrayOfResults[i] != int.MaxValue)
- {
- sum = sum + arrayOfResults[i];
- }
- }
- Console.WriteLine(sum);
- }
- }
- public static bool isCounted;
- private static int FindCost(int start, int finish, string inputCode, string validWord, string sortedWord)
- {
- string wordToCompare = inputCode.Substring(start, finish);
- char[] tempArr = wordToCompare.ToCharArray();
- Array.Sort(tempArr);
- string wordToCompareSorted = new string(tempArr);
- if (wordToCompareSorted == sortedWord)
- {
- isCounted = true;
- int counter = 0;
- for (int j = 0; j < wordToCompare.Length; j++)
- {
- if (wordToCompare[j] != validWord[j])
- {
- counter++;
- }
- }
- return counter;
- }
- else
- {
- return int.MaxValue;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement