Advertisement
VladoG

[Programming Fundamentals] - 01. Largest Common End-100/100

Jun 6th, 2016
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _01.Largest_Common_End_1
  8.     {
  9.     class LargestCommonEnd
  10.         {
  11.         static void Main(string[] args)
  12.             {
  13.             string[] str1 = Console.ReadLine().Split().ToArray();
  14.             string[] str2 = Console.ReadLine().Split().ToArray();
  15.  
  16.             //string[] str1 = { "hi", "php", "java", "csharp", "sql", "html", "css", "js" };
  17.             //string[] str2 = { "hi", "php", "java", "js", "softuni", "nakov", "java", "learn" };
  18.  
  19.             //string[] str1 = { "hi", "php", "java", "xml", "csharp", "sql", "html", "css", "js" };
  20.             //string[] str2 = { "nakov", "java", "sql", "html", "css", "js" };
  21.  
  22.             //Console.WriteLine(String.Join(" ", str1));
  23.             //Console.WriteLine(string.Join(" ", str2));
  24.  
  25.             int len1 = str1.Length;
  26.             int len2 = str2.Length;
  27.             int j = 0;
  28.             int countBegin = 0;
  29.             int countEnd = 0;
  30.             bool flagBegin = true;
  31.             bool flagEnd = true;
  32.  
  33.             for (int i = 0; i < len1; i++)
  34.                 {
  35.                 // Checking BEGIN arr
  36.                 if (str1[i] == str2[j] && flagBegin)
  37.                     {
  38.                     countBegin++;
  39.                     }
  40.                 else
  41.                     {
  42.                     flagBegin = false;
  43.                     }
  44.  
  45.                 // Checking END arr
  46.                 if (str1[len1 - 1 - i] == str2[len2 - 1 - j] && flagEnd)
  47.                     {
  48.                     countEnd++;
  49.                     }
  50.                 else
  51.                     {
  52.                     flagEnd = false;
  53.                     }
  54.  
  55.                 j++;
  56.                 if (j == len2)
  57.                     {
  58.                     j = 0;
  59.                     }
  60.  
  61.                 }
  62.  
  63.             if (countBegin >= countEnd)
  64.                 {
  65.                 Console.WriteLine(countBegin);
  66.                 }
  67.             else
  68.                 {
  69.                 Console.WriteLine(countEnd);
  70.                 }
  71.             }
  72.         }
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement