Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class StringMatrix
- {
- static void Main()
- {
- // direction arrays
- int[] dirRow = { 1, 0, 1, -1 };
- int[] dirCol = { 0, 1, 1, 1 };
- // The string array
- string[,] matrixStrings =
- {
- {"ha", "asd", "o", "w", "s","ha"},
- {"g", "ha", "sdf", "s", "","ha"},
- {"", "g", "ha", "sdf", "","ha"},
- {"s", "s", "", "ha", "","ha"},
- {"s", "", "s", "s", "ha","ha"},
- {"s", "", "s", "s", "ha","ha"},
- {"s", "", "s", "s", "ha","ha"},
- {"s", "", "s", "s", "ha","ha"},
- {"g", "ha", "sdf", "s", "","ha"},
- {"", "g", "ha", "sdf", "","ha"},
- };
- int BestLen = 0;
- string BestString = "";
- int numRows = matrixStrings.GetLength(0);
- int numCols = matrixStrings.GetLength(1);
- // Obhojdame masiva
- for (int row = 0; row < numRows; row++)
- {
- for (int col = 0; col < numCols; col++)
- {
- string currentStr = matrixStrings[row, col];
- // ispolzwame loop za smqna na posokata
- for (int direction = 0; direction < 4; direction++)
- {
- int currentRow = row;
- int currentCol = col;
- int currentLen = 1;
- while (true)
- {
- currentCol += dirCol[direction];
- currentRow += dirRow[direction];
- // proverqvame za narusheniq
- if (currentCol < 0 ||
- currentCol >= numCols ||
- currentRow < 0 ||
- currentRow >= numRows ||
- currentStr != matrixStrings[currentRow, currentCol])
- {
- break;
- }
- currentLen++;
- // proverqvame dali stringa na koito sme v momenta e po dobar ot naj dobriq
- if (currentLen > BestLen)
- {
- BestLen = currentLen;
- BestString = currentStr;
- }
- }
- }
- }
- }
- Console.WriteLine("The longest sequence of strings in the array is - {0}, with length - {1}", BestString, BestLen);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement