Advertisement
Guest User

Untitled

a guest
Sep 21st, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class SequenceInMatrix
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Console.WriteLine("Enter the matrix dimensions, separated by a space.");
  9.         int[] dimensions = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  10.         int n = dimensions[0];
  11.         int m = dimensions[1];
  12.  
  13.         string[,] matrix = new string[n, m];
  14.  
  15.         Console.WriteLine("Enter the strings in each row, separated by a space.");
  16.         for (int i = 0; i < n; i++)
  17.         {
  18.             string[] rowStrins = Console.ReadLine().Split(' ').ToArray();
  19.             for (int j = 0; j < m; j++)
  20.             {
  21.                 matrix[i, j] = rowStrins[j];
  22.             }
  23.         }
  24.  
  25.         int maxCount = 0;
  26.         string maxString = "";
  27.         for (int row = 0; row < n; row++)
  28.         {
  29.             for (int col = 0; col < m; col++)
  30.             {
  31.                 int countX = 0;
  32.                 int countY = 0;
  33.  
  34.                 while (row + countX < matrix.GetLength(0))
  35.                 {
  36.                     if (matrix[row, col] == matrix[row + countX, col])
  37.                     {
  38.                         countX++;
  39.                     }
  40.                     else
  41.                     {
  42.                         break;
  43.                     }
  44.                 }
  45.  
  46.                 if (countX + 1 > maxCount)
  47.                 {
  48.                     maxCount = countX;
  49.                     maxString = matrix[row, col];
  50.                 }
  51.  
  52.                 while (col + countY < matrix.GetLength(1))
  53.                 {
  54.                     if (matrix[row, col] == matrix[row, col + countY])
  55.                     {
  56.                         countY++;
  57.                     }
  58.                     else
  59.                     {
  60.                         break;
  61.                     }
  62.                 }
  63.  
  64.                 if (countY + 1 > maxCount)
  65.                 {
  66.                     maxCount = countY;
  67.                     maxString = matrix[row, col];
  68.                 }
  69.                 countX = 0;
  70.  
  71.                 while (row + countX < matrix.GetLength(0) && col + countX < matrix.GetLength(1))
  72.                 {
  73.                     if (matrix[row, col] == matrix[row + countX, col + countX])
  74.                     {
  75.                         countX++;
  76.                     }
  77.                     else
  78.                     {
  79.                         break;
  80.                     }
  81.                 }
  82.  
  83.                 if (countX + 1 > maxCount)
  84.                 {
  85.                     maxCount = countX;
  86.                     maxString = matrix[row, col];
  87.                 }
  88.             }
  89.         }
  90.  
  91.         Console.Write("The the longest sequence is:  {0}", maxString);
  92.         for (int i = 1; i < maxCount; i++)
  93.         {
  94.             Console.Write(",{0}", maxString);
  95.         }
  96.         Console.WriteLine();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement