Advertisement
sashomaga

Longest sequnce

Jan 10th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. //We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of several neighbor
  3. //elements located on the same line, column or diagonal. Write a program that finds the longest sequence of equal strings in the matrix
  4. class Program
  5. {
  6.  
  7.     static int bestMatch = 1;
  8.     static string bestString = "";
  9.  
  10.     static void Main()
  11.     {
  12.         string[,] matrix = {
  13.                         {"ha","yo","hi","hi","hi","jo"},
  14.                         {"ha","bo","yo","go","go","go"},
  15.                         {"ha","bo","li","yo","li","li"},
  16.                         {"bo","li","li","li","yo","li"},
  17.                         };
  18.  
  19.         for (int x = 0; x < matrix.GetLength(0); x++)
  20.         {
  21.             for (int y = 0; y < matrix.GetLength(1); y++)
  22.             {
  23.                 Seek(x, y, matrix);
  24.             }
  25.         }
  26.         //print result
  27.         for (int i = 0; i < bestMatch; i++)
  28.         {
  29.             if (i == 0)
  30.             {
  31.                 Console.Write(bestString);
  32.             }
  33.             else
  34.             {
  35.                 Console.Write("," + bestString);
  36.             }
  37.         }
  38.         Console.WriteLine();
  39.     }
  40.  
  41.     private static void Seek(int x, int y, string[,] matrix)
  42.     {
  43.         int match = 0;
  44.         string test = matrix[x, y];
  45.         //check horizontal
  46.         for (int row = x, col = y; col < matrix.GetLength(1); col++)
  47.         {
  48.             if (test == matrix[row, col])
  49.             {
  50.                 match++;
  51.             }
  52.         }
  53.         CheckForBestScore(match, test);
  54.         match = 0;
  55.         //check diagonal down
  56.         for (int row = x, col = y; row < matrix.GetLength(0) && col < matrix.GetLength(1); row++, col++)
  57.         {
  58.             if (test == matrix[row, col])
  59.             {
  60.                 match++;
  61.             }
  62.         }
  63.         CheckForBestScore(match, test);
  64.         match = 0;
  65.         //check diagonal up
  66.         for (int row = x, col = y; row >= 0 && col >= 0; row--, col--)
  67.         {
  68.             if (test == matrix[row, col])
  69.             {
  70.                 match++;
  71.             }
  72.         }
  73.         CheckForBestScore(match, test);
  74.         match = 0;
  75.         //check vertical
  76.         for (int row = x, col = y; row < matrix.GetLength(0); row++)
  77.         {
  78.             if (test == matrix[row, col])
  79.             {
  80.                 match++;
  81.             }
  82.         }
  83.         CheckForBestScore(match, test);
  84.     }
  85.  
  86.     private static void CheckForBestScore(int match, string test)
  87.     {
  88.         if (match > bestMatch)
  89.         {
  90.             bestMatch = match;
  91.             bestString = test;
  92.         }
  93.  
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement