Advertisement
ktopchiev

01. Largest Common End

Mar 7th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _01._Largest_Common_End
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string[] firstArray = Console.ReadLine().Split(' ').ToArray();
  11.             string[] secondArray = Console.ReadLine().Split(' ').ToArray();
  12.  
  13.             PrintTheLargestCommonEnd(firstArray, secondArray);
  14.         }
  15.  
  16.         static void PrintTheLargestCommonEnd(string[] firstArray, string[] secondArray)
  17.         {
  18.             var counterLeftToRight = ScanTheArraysFromLeftToRight(firstArray, secondArray);
  19.             var counterRightToLeft = ScanTheArraysFromRightToLeft(firstArray, secondArray);
  20.  
  21.             Console.WriteLine(counterLeftToRight + counterRightToLeft);
  22.            
  23.         }
  24.  
  25.         static int ScanTheArraysFromLeftToRight(string[] firstArray, string[] secondArray)
  26.         {
  27.             var counter = 0;
  28.  
  29.             for (int i = 0; i < Math.Min(firstArray.Length, secondArray.Length); i++)
  30.             {
  31.                 if (firstArray[i] == secondArray[i])
  32.                 {
  33.                     counter++;
  34.                 }
  35.  
  36.                 else
  37.                 {
  38.                     break;
  39.                 }
  40.             }
  41.  
  42.             return counter;
  43.         }
  44.  
  45.         static int ScanTheArraysFromRightToLeft(string[] firstArray, string[] secondArray)
  46.         {
  47.             var counter = 0;
  48.  
  49.             for (int i = Math.Max(firstArray.Length, secondArray.Length); i >= Math.Min(firstArray.Length, secondArray.Length) ; i--)
  50.             {
  51.                 if (firstArray.Length > secondArray.Length)
  52.                 {
  53.                     if (secondArray[i - Math.Abs(secondArray.Length - firstArray.Length) - 1] == firstArray[i - 1])
  54.                     {
  55.                         counter++;
  56.                     }
  57.                 }
  58.                 else if (secondArray.Length > firstArray.Length)
  59.                 {
  60.                     if (firstArray[i - Math.Abs(secondArray.Length - firstArray.Length) - 1] == secondArray[i - 1])
  61.                     {
  62.                         counter++;
  63.                     }
  64.                 }
  65.                 else
  66.                 {
  67.                     ScanTheArraysFromLeftToRight(firstArray, secondArray);
  68.                 }
  69.             }
  70.  
  71.             return counter;
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement