Advertisement
d_brezoev

SequenceOfEqualStringsInMatrixFinder

Dec 15th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 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 neighbor elements located on the same line, column or diagonal.
  2.  * Write a program that finds the longest sequence of equal strings in the matrix.  */
  3. using System;
  4.  
  5. namespace _03.EqualElementsInStringMatrix
  6. {
  7.     class EqualElementsInStringMatrix
  8.     {
  9.         static void CheckForBestLen(int currentCount, ref int bestCount, string element, ref string bestElement)
  10.         {
  11.             if (currentCount > bestCount)
  12.             {
  13.                 bestCount = currentCount;
  14.                 bestElement = element;
  15.             }
  16.         }
  17.         static int CountDiagonal(string[,] matrix, int row, int col)
  18.         {
  19.             int count = 1;
  20.             while (true)
  21.             {
  22.                 if (row + 1 == matrix.GetLength(0) || col+1 == matrix.GetLength(1) || matrix[row, col] != matrix[row + 1, col+1])
  23.                 {
  24.                     break;
  25.                 }
  26.                 row++;
  27.                 col++;
  28.                 count++;
  29.             }
  30.             return count;
  31.         }
  32.         static int CountVertical(string[,] matrix, int row, int col)
  33.         {
  34.             int count = 1;
  35.             while (true)
  36.             {
  37.                 if (row + 1 == matrix.GetLength(0) || matrix[row, col] != matrix[row+1, col])
  38.                 {
  39.                     break;
  40.                 }
  41.                 row++;
  42.                 count++;
  43.             }
  44.             return count;
  45.         }
  46.         static int CountHorizontal(string[,] matrix,int row, int col)
  47.         {
  48.             int count = 1;
  49.             while (true)
  50.             {
  51.                 if (col+1==matrix.GetLength(1)|| matrix[row,col]!=matrix[row,col+1])
  52.                 {
  53.                     break;
  54.                 }
  55.                 col++;
  56.                 count++;
  57.             }
  58.             return count;
  59.         }
  60.         static void PrintStringMatrix(string[,] matrix)
  61.         {
  62.             for (int row = 0; row < matrix.GetLength(0); row++)
  63.             {
  64.                 for (int col = 0; col < matrix.GetLength(1); col++)
  65.                 {
  66.                     Console.Write("{0,5}",matrix[row,col]);
  67.                 }
  68.                 Console.WriteLine();
  69.             }
  70.         }
  71.         static void Main(string[] args)
  72.         {
  73.             //string[] str = { "ha","ho","fifi","hi","xx","xxx"};
  74.             Console.WriteLine("Enter N:");
  75.             int n = int.Parse(Console.ReadLine());
  76.             Console.WriteLine("Enter M:");
  77.             int m = int.Parse(Console.ReadLine());
  78.             string[,] matrix = new string[n, m];
  79.             //Random rand = new Random();            
  80.             for (int row = 0; row < matrix.GetLength(0); row++)
  81.             {
  82.                 for (int col = 0; col < matrix.GetLength(1); col++)
  83.                 {
  84.                     //int index = rand.Next(0, str.Length);
  85.                     matrix[row, col] = Console.ReadLine();
  86.                 }
  87.             }
  88.             PrintStringMatrix(matrix);
  89.             int bestLen = 0;
  90.             string bestElement = string.Empty;
  91.             for (int row = 0; row < matrix.GetLength(0); row++)
  92.             {
  93.                 for (int col = 0; col < matrix.GetLength(1); col++)
  94.                 {
  95.                     string element = matrix[row, col];
  96.                     int currentLen = CountHorizontal(matrix, row, col);
  97.                     CheckForBestLen(currentLen, ref bestLen, element, ref bestElement);
  98.                     currentLen = CountVertical(matrix, row, col);
  99.                     CheckForBestLen(currentLen, ref bestLen, element, ref bestElement);
  100.                     currentLen = CountDiagonal(matrix, row, col);
  101.                     CheckForBestLen(currentLen, ref bestLen, element, ref bestElement);
  102.                 }
  103.             }
  104.             Console.WriteLine("Best length: {0}",bestLen);
  105.             Console.WriteLine("Element: {0}",bestElement);
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement