Advertisement
dimipan80

Sequence in Matrix

May 9th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. /* We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of several neighbour elements located on the same line, column or diagonal. Write a program that finds the longest sequence of equal strings in the matrix. */
  2.  
  3. namespace _04.SequenceInMatrix
  4. {
  5.     using System;
  6.     using System.Linq;
  7.  
  8.     class SequenceInMatrix
  9.     {
  10.         private static string[,] _matrix;
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             Console.WriteLine("Enter Sizes of your matrix, like two Integers, separated by a space: ");
  15.             int[] sizes =
  16.                 Console.ReadLine()
  17.                     .Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
  18.                     .Select(int.Parse)
  19.                     .ToArray();
  20.  
  21.             if (sizes.Length < 2 || sizes[0] < 1 || sizes[1] < 1)
  22.             {
  23.                 Console.WriteLine("Error!!! - Invalid input matrix!");
  24.                 return;
  25.             }
  26.  
  27.             _matrix = new string[sizes[0], sizes[1]];
  28.             Console.WriteLine("Enter your matrix content, each row on single line, each string, separated by a space: ");
  29.             for (int row = 0; row < _matrix.GetLength(0); row++)
  30.             {
  31.                 string[] words = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  32.                 if (words.Length != _matrix.GetLength(1))
  33.                 {
  34.                     Console.WriteLine("Error!!! - Invalid input matrix!");
  35.                     return;
  36.                 }
  37.  
  38.                 for (int col = 0; col < _matrix.GetLength(1); col++)
  39.                 {
  40.                     _matrix[row, col] = words[col];
  41.                 }
  42.             }
  43.  
  44.             FindLongestSequenceOfEqualsStringsInMatrix();
  45.         }
  46.  
  47.         private static void FindLongestSequenceOfEqualsStringsInMatrix()
  48.         {
  49.             int maxCounter = 0;
  50.             string longestRepeat = string.Empty;
  51.             for (int row = 0; row < _matrix.GetLength(0); row++)
  52.             {
  53.                 for (int col = 0; col < _matrix.GetLength(1); col++)
  54.                 {
  55.                     string currentStr = _matrix[row, col];
  56.                     int[] counts = new int[] { 1, 1, 1 };
  57.                     counts[0] = GetCountFromHorizontalSequence(row, col, currentStr);
  58.                     counts[1] = GetCountFromVerticalSequence(row, col, currentStr);
  59.                     counts[2] = GetCountFromDiagonalSequence(row, col, currentStr);
  60.                     if (counts.Max() > maxCounter)
  61.                     {
  62.                         maxCounter = counts.Max();
  63.                         longestRepeat = currentStr;
  64.                     }
  65.                 }
  66.             }
  67.  
  68.             PrintTheLongestSequenceOfEqualsStrings(longestRepeat, maxCounter);
  69.         }
  70.  
  71.         private static int GetCountFromHorizontalSequence(int y, int x, string word)
  72.         {
  73.             int counter = 1;
  74.             for (int col = x + 1; col < _matrix.GetLength(1); col++)
  75.             {
  76.                 if (_matrix[y, col] == word)
  77.                 {
  78.                     counter++;
  79.                 }
  80.             }
  81.             return counter;
  82.         }
  83.  
  84.         private static int GetCountFromVerticalSequence(int y, int x, string word)
  85.         {
  86.             int counter = 1;
  87.             for (int row = y + 1; row < _matrix.GetLength(0); row++)
  88.             {
  89.                 if (_matrix[row, x] == word)
  90.                 {
  91.                     counter++;
  92.                 }
  93.             }
  94.             return counter;
  95.         }
  96.  
  97.         private static int GetCountFromDiagonalSequence(int y, int x, string word)
  98.         {
  99.             int counter = 1;
  100.             for (int row = y + 1, col = x + 1; row < _matrix.GetLength(0) && col < _matrix.GetLength(1); row++, col++)
  101.             {
  102.                 if (_matrix[row, col] == word)
  103.                 {
  104.                     counter++;
  105.                 }
  106.             }
  107.  
  108.             return counter;
  109.         }
  110.  
  111.         private static void PrintTheLongestSequenceOfEqualsStrings(string word, int counter)
  112.         {
  113.             Console.WriteLine("The Longest sequence of equal strings in the matrix is: ");
  114.             for (int i = 0; i < counter; i++)
  115.             {
  116.                 Console.Write(word);
  117.                 if (i < counter - 1)
  118.                 {
  119.                     Console.Write(", ");
  120.                 }
  121.             }
  122.             Console.WriteLine();
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement