Advertisement
Guest User

LongestSequenceEqualStrings

a guest
Jan 9th, 2013
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 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 LongestSequenceEqualStrings
  8. {
  9.     class LongestSequenceEqualStrings
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             int m = int.Parse(Console.ReadLine());
  15.  
  16.             string[,] matrix = new string[n, m];
  17.             for (int i = 0; i < n; ++i)
  18.             {
  19.                 for (int j = 0; j < m; ++j)
  20.                 {
  21.                     matrix[i, j] = Console.ReadLine();
  22.                 }
  23.             }
  24.  
  25.             const int VerticalSequence = 0;
  26.             const int HorizontalSequence = 1;
  27.             const int DiagonalSequenceRight = 2;
  28.             const int DiagonalSequenceLeft = 3;
  29.  
  30.             int[] deltaX = new int[] {1, 0, 1,  1};
  31.             int[] deltaY = new int[] {0, 1, 1, -1};
  32.  
  33.             int[, ,] dp = new int[n, m, 4];
  34.  
  35.             int maximalLength = 0;
  36.             int bestRow = 0, bestCol = 0;
  37.  
  38.             for (int i = 0; i < n; ++i)
  39.             {
  40.                 for (int j = 0; j < m; ++j)
  41.                 {
  42.                     for (int k = 0; k < 4; ++k)
  43.                     {
  44.                         dp[i, j, k] = 1;
  45.                     }
  46.  
  47.                     if (i > 0 && matrix[i, j].Equals(matrix[i - 1, j]))
  48.                     {
  49.                         dp[i, j, VerticalSequence] = dp[i - 1, j, VerticalSequence] + 1;
  50.                     }
  51.                     if (j > 0 && matrix[i, j].Equals(matrix[i, j - 1]))
  52.                     {
  53.                         dp[i, j, HorizontalSequence] = dp[i, j - 1, HorizontalSequence] + 1;
  54.                     }
  55.                     if (i > 0 && j > 0 && matrix[i, j].Equals(matrix[i - 1, j - 1]))
  56.                     {
  57.                         dp[i, j, DiagonalSequenceRight] = dp[i - 1, j - 1, DiagonalSequenceRight] + 1;
  58.                     }
  59.                     if (i > 0 && j < n - 1 && matrix[i, j].Equals(matrix[i - 1, j + 1]))
  60.                     {
  61.                         dp[i, j, DiagonalSequenceLeft] = dp[i - 1, j + 1, DiagonalSequenceLeft] + 1;
  62.                     }
  63.  
  64.                     for (int k = 0; k < 4; ++k)
  65.                     {
  66.                         if (dp[i, j, k] > maximalLength)
  67.                         {
  68.                             maximalLength = dp[i, j, k];
  69.                             bestRow = i;
  70.                             bestCol = j;
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             Console.WriteLine("The longest sequence is with length: " + maximalLength);
  77.             Console.WriteLine("The value of the repeating string is: " + matrix[bestRow, bestCol]);
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement