Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _03.BestSequenceOfStrings
- {
- class Program
- {
- static int bestSequence = 0;
- static string bestElement = String.Empty;
- static int rowsCount;
- static int colsCount;
- static void Main(string[] args)
- {
- string[,] matrix =
- {
- {"ha","ho","hA","hO","xxx","he"},
- {"fo","hAa","hA","xxx","hS","he"},
- {"xxx","ho","xxx","xx","hS","he"},
- {"xxx","xxx","xx","ha","hS","he"},
- {"xxx","ho","ha","xx","ha","hE"},
- };
- rowsCount = matrix.GetLength(0);
- colsCount = matrix.GetLength(1);
- for (int i = 0; i < rowsCount; i++)
- {
- for (int j = 0; j < colsCount; j++)
- {
- CheckMatrix(matrix, i, j);
- }
- }
- Console.WriteLine("BestSequence: {0}", bestSequence);
- Console.WriteLine("BestElement: {0}", bestElement);
- }
- private static void CheckMatrix(string[,] matrix, int rows, int cols)
- {
- int startingRows = rows;
- int startingCols = cols;
- string searchingString = matrix[rows, cols];
- //1-right rows is constant cols+1
- //2-down rows+1 cols is constant
- //3-diagonalDown starting row from zero to .Length rows+1 cols+1
- //4-diagonalDown starting row from .Length to zerp rows-1 cols+1
- //right
- int counter = 0;
- while (cols + 1<= colsCount && (String.Compare(searchingString, matrix[rows, cols]) == 0))
- {
- cols++;
- counter++;
- }
- CheckBest(counter,searchingString);
- //down
- counter = 0;
- rows = startingRows;
- cols = startingCols;
- while (rows +1 <= rowsCount && (String.Compare(searchingString, matrix[rows, cols]) == 0))
- {
- rows++;
- counter++;
- }
- CheckBest(counter, searchingString);
- //diagonalDown
- counter = 0;
- rows = startingRows;
- cols = startingCols;
- while (rows + 1 <= rowsCount && cols+1 <=colsCount && (String.Compare(searchingString, matrix[rows, cols]) == 0))
- {
- rows++;
- cols++;
- counter++;
- }
- CheckBest(counter, searchingString);
- //diagonalUP
- counter = 0;
- rows = startingRows;
- cols = startingCols;
- while (rows >= 0 && cols + 1 <= colsCount && (String.Compare(searchingString, matrix[rows, cols]) == 0))
- {
- rows--;
- cols++;
- counter++;
- }
- CheckBest(counter, searchingString);
- }
- private static void CheckBest(int counter, string element)
- {
- if (counter>=bestSequence)
- {
- bestSequence = counter;
- bestElement = element;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement