Advertisement
Filkolev

Nakovs Matching

Jul 26th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2.  
  3. class NakovsMatching
  4. {
  5.     static void Main()
  6.     {
  7.         string firstString = Console.ReadLine();
  8.         string secondString = Console.ReadLine();
  9.  
  10.         int limit = int.Parse(Console.ReadLine());
  11.  
  12.         bool found = false;
  13.  
  14.         for (int i = 1; i < firstString.Length; i++)
  15.         {            
  16.             for (int j = 1; j < secondString.Length; j++)
  17.             {
  18.                 string firstSubstring = firstString.Substring(0, i);
  19.                 string secondSubstring = firstString.Substring(i);                
  20.                 string thirdSubstring = secondString.Substring(0, j);
  21.                 string fourthSubstring = secondString.Substring(j);
  22.  
  23.                 int weight = calculateWeight(firstSubstring) * calculateWeight(fourthSubstring) - calculateWeight(secondSubstring) * calculateWeight(thirdSubstring);
  24.  
  25.                 if (Math.Abs(weight) <= limit)
  26.                 {
  27.                     found = true;
  28.                     Console.WriteLine("({0}|{1}) matches ({2}|{3}) by {4} nakovs", firstSubstring, secondSubstring, thirdSubstring, fourthSubstring, Math.Abs(weight));
  29.                 }
  30.             }
  31.         }
  32.  
  33.         if (!found)
  34.         {
  35.             Console.WriteLine("No");
  36.         }
  37.     }
  38.  
  39.     public static int calculateWeight(string substring)
  40.     {
  41.         int weight = 0;
  42.  
  43.         for (int i = 0; i < substring.Length; i++)
  44.         {
  45.             weight += substring[i];
  46.         }
  47.  
  48.         return weight;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement