Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of several neighbour elements located on the same line, column or diagonal. Write a program that finds the longest sequence of equal strings in the matrix. */
- namespace _04.SequenceInMatrix
- {
- using System;
- using System.Linq;
- class SequenceInMatrix
- {
- private static string[,] _matrix;
- static void Main(string[] args)
- {
- Console.WriteLine("Enter Sizes of your matrix, like two Integers, separated by a space: ");
- int[] sizes =
- Console.ReadLine()
- .Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- if (sizes.Length < 2 || sizes[0] < 1 || sizes[1] < 1)
- {
- Console.WriteLine("Error!!! - Invalid input matrix!");
- return;
- }
- _matrix = new string[sizes[0], sizes[1]];
- Console.WriteLine("Enter your matrix content, each row on single line, each string, separated by a space: ");
- for (int row = 0; row < _matrix.GetLength(0); row++)
- {
- string[] words = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
- if (words.Length != _matrix.GetLength(1))
- {
- Console.WriteLine("Error!!! - Invalid input matrix!");
- return;
- }
- for (int col = 0; col < _matrix.GetLength(1); col++)
- {
- _matrix[row, col] = words[col];
- }
- }
- FindLongestSequenceOfEqualsStringsInMatrix();
- }
- private static void FindLongestSequenceOfEqualsStringsInMatrix()
- {
- int maxCounter = 0;
- string longestRepeat = string.Empty;
- for (int row = 0; row < _matrix.GetLength(0); row++)
- {
- for (int col = 0; col < _matrix.GetLength(1); col++)
- {
- string currentStr = _matrix[row, col];
- int[] counts = new int[] { 1, 1, 1 };
- counts[0] = GetCountFromHorizontalSequence(row, col, currentStr);
- counts[1] = GetCountFromVerticalSequence(row, col, currentStr);
- counts[2] = GetCountFromDiagonalSequence(row, col, currentStr);
- if (counts.Max() > maxCounter)
- {
- maxCounter = counts.Max();
- longestRepeat = currentStr;
- }
- }
- }
- PrintTheLongestSequenceOfEqualsStrings(longestRepeat, maxCounter);
- }
- private static int GetCountFromHorizontalSequence(int y, int x, string word)
- {
- int counter = 1;
- for (int col = x + 1; col < _matrix.GetLength(1); col++)
- {
- if (_matrix[y, col] == word)
- {
- counter++;
- }
- }
- return counter;
- }
- private static int GetCountFromVerticalSequence(int y, int x, string word)
- {
- int counter = 1;
- for (int row = y + 1; row < _matrix.GetLength(0); row++)
- {
- if (_matrix[row, x] == word)
- {
- counter++;
- }
- }
- return counter;
- }
- private static int GetCountFromDiagonalSequence(int y, int x, string word)
- {
- int counter = 1;
- for (int row = y + 1, col = x + 1; row < _matrix.GetLength(0) && col < _matrix.GetLength(1); row++, col++)
- {
- if (_matrix[row, col] == word)
- {
- counter++;
- }
- }
- return counter;
- }
- private static void PrintTheLongestSequenceOfEqualsStrings(string word, int counter)
- {
- Console.WriteLine("The Longest sequence of equal strings in the matrix is: ");
- for (int i = 0; i < counter; i++)
- {
- Console.Write(word);
- if (i < counter - 1)
- {
- Console.Write(", ");
- }
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement