ellapt

T8.3.LongestStrSeq

Jan 17th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. using System;
  2.  
  3. class LongestStrSeq
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Find the longest sequence of equal strings in a matrix N x M");
  8. Console.Write("Enter number of rows of the matrix: ");
  9. int rSize = int.Parse(Console.ReadLine());
  10. Console.WriteLine("Enter number of columns of the matrix to be printed: ");
  11. int cSize = int.Parse(Console.ReadLine());
  12. string[,] matrix = new string[rSize, cSize];
  13. Console.WriteLine("Enter the elements of the matrix to be printed: ");
  14. for (int i = 0; i < rSize; i++)
  15. {
  16. for (int j = 0; j < cSize; j++)
  17. {
  18. matrix[i, j] = Console.ReadLine();
  19. }
  20. }
  21. int seqRow = 0, seqCol = 0;
  22. int max = 0;
  23. int[, ,] dim3 = new int[rSize, cSize, 4];
  24. int dirX = 1;
  25. int dirY = 0;
  26. int LeftDiag = 3;
  27. int RightDiag = 2;
  28.  
  29. for (int i = 0; i < rSize; ++i)
  30. {
  31. for (int j = 0; j < cSize; ++j)
  32. {
  33. for (int k = 0; k < 4; ++k)
  34. {
  35. dim3[i, j, k] = 1;
  36. }
  37.  
  38. if (i > 0 && matrix[i, j].Equals(matrix[i - 1, j]))
  39. {
  40. dim3[i, j, dirY] = dim3[i - 1, j, dirY] + 1;
  41. }
  42. if (j > 0 && matrix[i, j].Equals(matrix[i, j - 1]))
  43. {
  44. dim3[i, j, dirX] = dim3[i, j - 1, dirX] + 1;
  45. }
  46. if (i > 0 && j > 0 && matrix[i, j].Equals(matrix[i - 1, j - 1]))
  47. {
  48. dim3[i, j, RightDiag] = dim3[i - 1, j - 1, RightDiag] + 1;
  49. }
  50. if (i > 0 && j < rSize - 1 && matrix[i, j].Equals(matrix[i - 1, j + 1]))
  51. {
  52. dim3[i, j, LeftDiag] = dim3[i - 1, j + 1, LeftDiag] + 1;
  53. }
  54.  
  55. for (int k = 0; k < 4; ++k)
  56. {
  57. if (dim3[i, j, k] > max)
  58. {
  59. max = dim3[i, j, k];
  60. seqRow = i;
  61. seqCol = j;
  62. }
  63. }
  64. }
  65. }
  66.  
  67. Console.WriteLine("The length of the longest sequence of equal strings is : " + max);
  68. Console.WriteLine("The string is: " + matrix[seqRow, seqCol]);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment