Advertisement
Guest User

Untitled

a guest
May 8th, 2015
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SequenceMatrix_4
  8. {
  9.     class SequenceMatrix_4
  10.     {
  11.         static string[,] matrix;
  12.         static string currentString = String.Empty;
  13.  
  14.         static void LongestSequence()
  15.         {
  16.             int longestSeq = 0;
  17.             string maxRepeatable = String.Empty;
  18.             for (int i = 0; i < matrix.GetLength(0); i++)
  19.             {
  20.                 for (int j = 0; j < matrix.GetLength(1); j++)
  21.                 {
  22.                     currentString = matrix[i, j];
  23.                     int rowCount = CountRow(i, j);
  24.                     int colCount = CountCol(i, j);
  25.                     int diagCount = CountDiagonal(i, j);
  26.                     int temp = Math.Max(Math.Max(rowCount, colCount), diagCount);
  27.                     if (temp > longestSeq)
  28.                     {
  29.                         longestSeq = temp;
  30.                         maxRepeatable = currentString;
  31.                     }
  32.                 }
  33.             }
  34.             PrintResult(longestSeq,maxRepeatable);
  35.         }
  36.         static int CountRow(int i,int j)
  37.         {
  38.             int counter = 1;
  39.             for (int a = j + 1; a < matrix.GetLength(1); a++)
  40.             {
  41.                 if (matrix[i, a] == currentString)
  42.                 {
  43.                     counter++;
  44.                 }
  45.             }
  46.             return counter;
  47.         }
  48.         static int CountCol(int i, int j)
  49.         {
  50.             int counter = 1;
  51.             for (int a = i + 1; a < matrix.GetLength(0); a++)
  52.             {
  53.                 if (matrix[a, j] == currentString)
  54.                 {
  55.                     counter++;
  56.                 }
  57.             }
  58.             return counter;
  59.         }
  60.         static int CountDiagonal(int i,int j)
  61.         {
  62.             int counter = 1;
  63.             int diagonal = (matrix.GetLength(0) < matrix.GetLength(1)) ? matrix.GetLength(0) : matrix.GetLength(1);
  64.             for (int a = j + 1; a < diagonal; a++)
  65.             {
  66.                 if (matrix[a, a] == currentString)
  67.                 {
  68.                     counter++;
  69.                 }
  70.             }
  71.             return counter;
  72.         }
  73.         static void PrintResult(int count,string word)
  74.         {
  75.             for (int a = 0; a < matrix.GetLength(0); a++)
  76.             {
  77.                 for (int b = 0; b < matrix.GetLength(1); b++)
  78.                 {
  79.                     Console.Write("|{0,4}|",matrix[a,b]);
  80.                 }
  81.                 Console.WriteLine();
  82.             }
  83.             Console.WriteLine();
  84.             Console.WriteLine("longest sequence is:");
  85.             for (int c = 0; c < count; c++)
  86.             {
  87.                 Console.Write("{0},",word);
  88.             }
  89.             Console.WriteLine();
  90.         }
  91.         static void Main(string[] args)
  92.         {
  93.             int row = int.Parse(Console.ReadLine());
  94.             int col = int.Parse(Console.ReadLine());
  95.             matrix = new string[row, col];
  96.  
  97.             for (int a = 0; a < row; a++)
  98.             {
  99.                 for (int b = 0; b < col; b++)
  100.                 {
  101.                     Console.Write("Enter [{0}, {1}] element: ", a, b);
  102.                     matrix[a, b] = Console.ReadLine();
  103.                 }
  104.             }
  105.             LongestSequence();
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement