Advertisement
tockata

04_NakovsMatching

Jul 27th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class NakovsMatching
  5. {
  6.     static void Main()
  7.     {
  8.         string firstWord = Console.ReadLine();
  9.         string secondWord = Console.ReadLine();
  10.         long d = long.Parse(Console.ReadLine());
  11.         bool matchFound = false;
  12.  
  13.         for (int i = 0; i < firstWord.Length - 1; i++)
  14.         {
  15.             long firstWordLeftSum = 0;
  16.             long firstWordRightSum = 0;
  17.             List<char> firstLeft = new List<char>();
  18.             List<char> firstRight = new List<char>();
  19.             int rightStartIndexFirstWord = i + 1;
  20.  
  21.             for (int k = 0; k < rightStartIndexFirstWord; k++)
  22.             {
  23.                 firstWordLeftSum += firstWord[k];
  24.                 firstLeft.Add(firstWord[k]);
  25.             }
  26.             for (int m = rightStartIndexFirstWord; m < firstWord.Length; m++)
  27.             {
  28.                 firstWordRightSum += firstWord[m];
  29.                 firstRight.Add(firstWord[m]);
  30.             }
  31.             for (int j = 0; j < secondWord.Length - 1; j++)
  32.             {
  33.                 long secondWordLeftSum = 0;
  34.                 long secondWordRightSum = 0;
  35.                 List<char> secondLeft = new List<char>();
  36.                 List<char> secondRight = new List<char>();
  37.                 int rightStartIndexSecondWord = j + 1;
  38.  
  39.                 for (int n = 0; n < rightStartIndexSecondWord; n++)
  40.                 {
  41.                     secondWordLeftSum += secondWord[n];
  42.                     secondLeft.Add(secondWord[n]);
  43.                 }
  44.                 for (int p = rightStartIndexSecondWord; p < secondWord.Length; p++)
  45.                 {
  46.                     secondWordRightSum += secondWord[p];
  47.                     secondRight.Add(secondWord[p]);
  48.                 }
  49.                 long diff = Math.Abs(firstWordLeftSum * secondWordRightSum - firstWordRightSum * secondWordLeftSum);
  50.                 if (diff <= d)
  51.                 {
  52.                     string firstResult = string.Join("", firstLeft) + "|" + string.Join("", firstRight);
  53.                     string secondResult = string.Join("", secondLeft) + "|" + string.Join("", secondRight);
  54.                     Console.WriteLine("({0}) matches ({1}) by {2} nakovs", firstResult, secondResult, diff);
  55.                     matchFound = true;
  56.                 }
  57.             }
  58.         }
  59.         if (matchFound == false)
  60.         {
  61.             Console.WriteLine("No");
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement