Advertisement
BackoChan

String Sequence in a Matrix

Dec 18th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5.  
  6. class SequencesInTheMatrix
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         Console.Write("Enter N: ");
  11.         int n = int.Parse(Console.ReadLine());
  12.         Console.Write("Enter M: ");
  13.         int m = int.Parse(Console.ReadLine());
  14.         string[,] matrix = new string[n, m];
  15.            
  16.         //fill the matrix
  17.         for (int row = 0; row < matrix.GetLength(0); row++)
  18.         {
  19.             for (int col = 0; col < matrix.GetLength(1); col++)
  20.             {
  21.                 string input = Console.ReadLine();
  22.                 matrix[row, col] = input;
  23.             }
  24.         }
  25.         // print the matrix
  26.         Console.WriteLine("This is the matrix !");
  27.         for (int row = 0; row < matrix.GetLength(0); row++)
  28.         {
  29.             for (int col = 0; col < matrix.GetLength(1); col++)
  30.             {
  31.                 Console.Write(matrix[row, col] + " ");
  32.             }
  33.             Console.WriteLine();
  34.         }
  35.  
  36.         //scan the rows for a sequence
  37.         List<string> sequence = new List<string>();
  38.         List<string> maximumSequence = new List<string>();
  39.         bool endOfSequence = false;
  40.         int currentSequence = 0;
  41.         int maxSequence = 0;
  42.         for (int row = 0; row < matrix.GetLength(0); row++)
  43.         {
  44.             for (int col = 0; col < matrix.GetLength(1) - 1; col++)
  45.             {
  46.                 if (matrix[row, col] == matrix[row, col + 1])
  47.                 {
  48.                    
  49.                     currentSequence++;
  50.                     sequence.Add(matrix[row, col]);
  51.                     if (currentSequence > maxSequence)
  52.                     {
  53.                         maxSequence = currentSequence;
  54.                        
  55.                         maximumSequence = new List<string>(sequence);
  56.                        
  57.                     }
  58.                     endOfSequence = true;
  59.                 }
  60.                 else if (endOfSequence)
  61.                 {
  62.                    
  63.                     endOfSequence = false;
  64.                     currentSequence = 0;
  65.                     sequence.Clear();
  66.                    
  67.                 }
  68.                
  69.             }
  70.             currentSequence = 0;
  71.         }
  72.         //scan the columns for a sequence.
  73.         currentSequence = 0;
  74.         sequence = new List<string>();
  75.         endOfSequence = false;
  76.         for (int col = 0; col < matrix.GetLength(1); col++)
  77.         {
  78.             for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  79.             {
  80.                 if (matrix[row, col] == matrix[row + 1, col])
  81.                 {
  82.                     currentSequence++;
  83.                     sequence.Add(matrix[row, col]);
  84.                     if (currentSequence > maxSequence)
  85.                     {
  86.                         maxSequence = currentSequence;
  87.  
  88.                         maximumSequence = new List<string>(sequence);
  89.  
  90.                     }
  91.                     endOfSequence = true;
  92.                 }
  93.                 else if (endOfSequence)
  94.                 {
  95.                    
  96.                     endOfSequence = false;
  97.                     currentSequence = 0;
  98.                     sequence.Clear();
  99.                    
  100.                 }
  101.             }
  102.         }
  103.         //scan the right for the sequence
  104.         currentSequence = 0;
  105.         sequence = new List<string>();
  106.         endOfSequence = false;
  107.  
  108.         for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  109.         {
  110.             for (int col = 0; col < matrix.GetLength(1) - 1; col++)
  111.             {
  112.                 int moveRow = row;
  113.                 int moveCol = col;
  114.                 while (moveRow != n - 1 || moveCol != m - 1)
  115.                 {
  116.                     if (moveRow == n - 1 || moveCol == m - 1)
  117.                     {
  118.                         break;
  119.                     }
  120.                     if (matrix[moveRow, moveCol] == matrix[moveRow + 1, moveCol + 1])
  121.                     {
  122.                         currentSequence++;
  123.                         sequence.Add(matrix[moveRow, moveCol]);
  124.                        
  125.                         if (currentSequence > maxSequence)
  126.                         {
  127.                             maxSequence = currentSequence;
  128.  
  129.                             maximumSequence = new List<string>(sequence);
  130.  
  131.                         }
  132.                         endOfSequence = true;
  133.                     }
  134.                     else if (endOfSequence)
  135.                     {
  136.  
  137.                         endOfSequence = false;
  138.                         currentSequence = 0;
  139.                         sequence.Clear();
  140.                         break;
  141.  
  142.                     }
  143.                     else
  144.                     {
  145.                         break;
  146.                     }
  147.                     moveRow++;
  148.                     moveCol++;
  149.                    
  150.                 }
  151.                
  152.             }
  153.         }
  154.         //scan the left diagonal
  155.         currentSequence = 0;
  156.         sequence = new List<string>();
  157.         endOfSequence = false;
  158.         for (int row = 0 ; row < matrix.GetLength(0); row++)
  159.         {
  160.             for (int col = matrix.GetLength(1) - 1; col >= 1; col--)
  161.             {
  162.                 int moveRow = row;
  163.                 int moveCol = col;
  164.                 while (moveRow != n - 1 || moveCol != 0)
  165.                 {
  166.                     if (moveRow == n - 1 || moveCol == 0)
  167.                     {
  168.                         break;
  169.                     }
  170.                     if (matrix[moveRow, moveCol] == matrix[moveRow + 1, moveCol - 1])
  171.                     {
  172.                         currentSequence++;
  173.                         sequence.Add(matrix[moveRow, moveCol]);
  174.  
  175.                         if (currentSequence > maxSequence)
  176.                         {
  177.                             maxSequence = currentSequence;
  178.  
  179.                             maximumSequence = new List<string>(sequence);
  180.  
  181.                         }
  182.                         endOfSequence = true;
  183.                     }
  184.                     else if (endOfSequence)
  185.                     {
  186.  
  187.                         endOfSequence = false;
  188.                         currentSequence = 0;
  189.                         sequence.Clear();
  190.                         break;
  191.  
  192.                     }
  193.                     else
  194.                     {
  195.                         break;
  196.                     }
  197.                     moveRow++;
  198.                     moveCol--;
  199.                 }
  200.             }
  201.         }
  202.         //print out the max sequence
  203.         if (maximumSequence.Count > 0)
  204.         {
  205.             maximumSequence.Add(maximumSequence[0]); // add the first element again.
  206.             Console.Write("The longest sequnce is: ");
  207.             for (int i = 0; i < maximumSequence.Count; i++)
  208.             {
  209.                 Console.Write(maximumSequence[i] + " ");
  210.             }
  211.         }
  212.         else
  213.         {
  214.             Console.WriteLine("There is no sequence");
  215.         }
  216.         Console.WriteLine();
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement