alexmitev

Untitled

Nov 25th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 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. //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. Examples:
  8.  
  9. class FindTheLongestSequence
  10. {
  11.     static void Main()
  12.     {
  13.         List<string> resultList = new List<string>();
  14.         List<string> currentList = new List<string>();
  15.  
  16.         var matrix = new string[,] {{"ha","fifi","ho","hi"}, {"fo","ha","hi","xx"},{"xxx", "ho", "ha","xx"}};
  17.  
  18.         for (int row = 0; row < matrix.GetLength(0); row++)
  19.         {
  20.             int index = 0;
  21.             for (int col = 0; col < matrix.GetLength(1); col++)
  22.             {
  23.                 //here matrix[row, col] is our starting position;
  24.  
  25.                 for (int i = 0; i < matrix.GetLength(1) - col; i++) //check horizontally
  26.                 {
  27.                     if (matrix[row, col + i] != matrix[row, col])
  28.                     {
  29.                         break;
  30.                     }
  31.                     currentList.Add(matrix[row, col]);
  32.                     }
  33.                 if (currentList.Count > resultList.Count)
  34.                 {
  35.                     resultList = currentList.ToList(); //this keeps our current identical sequence.
  36.                 }
  37.                 currentList.Clear();
  38.                 for (int i = 0; i < matrix.GetLength(0) - row; i++) // check vertically
  39.                 {
  40.                     if (matrix[row + i, col] != matrix[row, col])
  41.                     {
  42.                         break;
  43.                     }
  44.                     currentList.Add(matrix[row, col]);
  45.                 }
  46.                 if (currentList.Count > resultList.Count)
  47.                 {
  48.                     resultList = currentList.ToList();
  49.                 }
  50.                 currentList.Clear();
  51.                 while (row + index < matrix.GetLength(0) && col + index < matrix.GetLength(1) && matrix[row + index, col + index] == matrix[row,col]) //check left-right diagonal
  52.                 {
  53.                     currentList.Add(matrix[row, col]);
  54.                     index++;
  55.                 }
  56.                 if (currentList.Count > resultList.Count)
  57.                 {
  58.                     resultList = currentList.ToList();
  59.                 }
  60.                 currentList.Clear();
  61.                 index = 0;
  62.                 while (row + index < matrix.GetLength(0) && col - index >= 0 && matrix[row + index, col - index] == matrix[row,col]) //check right-left giagonal
  63.                 {
  64.                     currentList.Add(matrix[row, col]);
  65.                     index++;
  66.                 }
  67.                 if (currentList.Count > resultList.Count)
  68.                 {
  69.                     resultList = currentList.ToList();
  70.                 }
  71.                 currentList.Clear();
  72.                 index = 0;
  73.             }
  74.         }
  75.         Console.WriteLine(string.Join(" ", resultList));
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment