Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _01._Largest_Common_End
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] firstArray = Console.ReadLine().Split(' ').ToArray();
- string[] secondArray = Console.ReadLine().Split(' ').ToArray();
- PrintTheLargestCommonEnd(firstArray, secondArray);
- }
- static void PrintTheLargestCommonEnd(string[] firstArray, string[] secondArray)
- {
- var counterLeftToRight = ScanTheArraysFromLeftToRight(firstArray, secondArray);
- var counterRightToLeft = ScanTheArraysFromRightToLeft(firstArray, secondArray);
- Console.WriteLine(counterLeftToRight + counterRightToLeft);
- }
- static int ScanTheArraysFromLeftToRight(string[] firstArray, string[] secondArray)
- {
- var counter = 0;
- for (int i = 0; i < Math.Min(firstArray.Length, secondArray.Length); i++)
- {
- if (firstArray[i] == secondArray[i])
- {
- counter++;
- }
- else
- {
- break;
- }
- }
- return counter;
- }
- static int ScanTheArraysFromRightToLeft(string[] firstArray, string[] secondArray)
- {
- var counter = 0;
- for (int i = Math.Max(firstArray.Length, secondArray.Length); i >= Math.Min(firstArray.Length, secondArray.Length) ; i--)
- {
- if (firstArray.Length > secondArray.Length)
- {
- if (secondArray[i - Math.Abs(secondArray.Length - firstArray.Length) - 1] == firstArray[i - 1])
- {
- counter++;
- }
- }
- else if (secondArray.Length > firstArray.Length)
- {
- if (firstArray[i - Math.Abs(secondArray.Length - firstArray.Length) - 1] == secondArray[i - 1])
- {
- counter++;
- }
- }
- else
- {
- ScanTheArraysFromLeftToRight(firstArray, secondArray);
- }
- }
- return counter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement