Advertisement
Teodor92

StringMatrix

Sep 25th, 2012
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class StringMatrix
  5. {
  6.     static void Main()
  7.     {
  8.         // direction arrays
  9.         int[] dirRow = { 1, 0, 1, -1 };
  10.         int[] dirCol = { 0, 1, 1, 1 };
  11.         // The string array
  12.         string[,] matrixStrings =
  13.                 {
  14.                     {"ha", "asd", "o", "w", "s","ha"},
  15.                     {"g", "ha", "sdf", "s", "","ha"},
  16.                     {"", "g", "ha", "sdf", "","ha"},
  17.                     {"s", "s", "", "ha", "","ha"},
  18.                     {"s", "", "s", "s", "ha","ha"},
  19.                     {"s", "", "s", "s", "ha","ha"},
  20.                     {"s", "", "s", "s", "ha","ha"},
  21.                     {"s", "", "s", "s", "ha","ha"},
  22.                     {"g", "ha", "sdf", "s", "","ha"},
  23.                     {"", "g", "ha", "sdf", "","ha"},
  24.                 };
  25.         int BestLen = 0;
  26.         string BestString = "";
  27.         int numRows = matrixStrings.GetLength(0);
  28.         int numCols = matrixStrings.GetLength(1);
  29.         // Obhojdame masiva
  30.         for (int row = 0; row < numRows; row++)
  31.         {
  32.             for (int col = 0; col < numCols; col++)
  33.             {
  34.                 string currentStr = matrixStrings[row, col];
  35.                 // ispolzwame loop za smqna na posokata
  36.                 for (int direction = 0; direction < 4; direction++)
  37.                 {
  38.                     int currentRow = row;
  39.                     int currentCol = col;
  40.                     int currentLen = 1;
  41.                     while (true)
  42.                     {
  43.                         currentCol += dirCol[direction];
  44.                         currentRow += dirRow[direction];
  45.                         // proverqvame za narusheniq
  46.                         if (currentCol < 0 ||
  47.                             currentCol >= numCols ||
  48.                             currentRow < 0 ||
  49.                             currentRow >= numRows ||
  50.                             currentStr != matrixStrings[currentRow, currentCol])
  51.                         {
  52.                             break;
  53.                         }
  54.                         currentLen++;
  55.                         // proverqvame dali stringa na koito sme v momenta e po dobar ot naj dobriq
  56.                         if (currentLen > BestLen)
  57.                         {
  58.                             BestLen = currentLen;
  59.                             BestString = currentStr;
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.         Console.WriteLine("The longest sequence of strings in the array is - {0}, with length - {1}", BestString, BestLen);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement