Advertisement
Guest User

Untitled

a guest
Dec 29th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 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. //We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of several neighbor elements located
  8. //on the same line, column or diagonal. Write a program that finds the longest sequence of equal strings in the matrix. Example:
  9.  
  10.  
  11. class StringMatrixMostFrequent
  12. {
  13. static string[,] ConsoleReadStringMatrix(int matrixRows, int matrixColumns)
  14. {
  15. string[,] result = new string[matrixRows, matrixColumns];
  16. string message = @"Please enter each row of the matrix in one line and separate each string with semicolumns "";"" and or space "" "" " ;
  17. Console.WriteLine(message);
  18.  
  19. char[] separators = { ';' , ' '};
  20.  
  21. for (int rows = 0; rows < matrixRows; rows++)
  22. {
  23. string line = Console.ReadLine();
  24. string[] actualInput = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  25.  
  26. for (int cols = 0; cols < matrixColumns; cols++)
  27. {
  28. result[rows, cols] = actualInput[cols];
  29. }
  30. }
  31.  
  32. return result;
  33. }
  34.  
  35. //static void ConsolePrintStringMatrix(string[,] matrix)
  36. //{
  37. // for (int rows = 0; rows < matrix.GetLength(0); rows++)
  38. // {
  39. // for (int cols = 0; cols < matrix.GetLength(1); cols++)
  40. // {
  41. // Console.Write("{0} ", matrix[rows, cols]);
  42. // }
  43. // Console.WriteLine();
  44. // }
  45. //}
  46.  
  47.  
  48.  
  49. static void Main(string[] args)
  50. {
  51. int n = int.Parse(Console.ReadLine());
  52. int m = int.Parse(Console.ReadLine());
  53. string[,] matrix = ConsoleReadStringMatrix(n, m);
  54.  
  55. //string[,] matrix = {
  56. // {"s", "a", "da", "blq", "q"},
  57. // {"pp", "ss", "s", "q", "l"},
  58. // {"p", "pp", "q", "k", "l"},
  59. // {"g", "q", "da", "blq", "k"},
  60. // {"q", "ne", "ne", "ne","ne"}
  61. // };
  62.  
  63. //find the longest sequence of strings in a column
  64. //string arc = "arc"; // use this to mark a cell as already checked;
  65.  
  66. int maxCounter = int.MinValue;
  67. string result = null; // string which makes the longest sequence
  68. for (int rows = 0; rows < matrix.GetLength(0); rows++)
  69. {
  70. for (int cols = 0; cols < matrix.GetLength(1); cols++)
  71. {
  72. int counter = 1;
  73.  
  74. int currentRow = rows + 1;
  75.  
  76. if (currentRow == matrix.GetLength(0)) break; // if we are at the last element in the matrix stop;
  77. if(matrix[rows, cols] == matrix[currentRow, cols])
  78. {
  79. while (currentRow < matrix.GetLength(0) && matrix[currentRow, cols] == matrix[rows, cols])
  80. {
  81. currentRow ++;
  82. counter ++;
  83. }
  84. }
  85.  
  86. if (counter > maxCounter)
  87. {
  88. maxCounter = counter;
  89. result = matrix[rows, cols];
  90. }
  91. }
  92. }
  93.  
  94. //Console.WriteLine("{0} {1}", result, maxCounter);
  95.  
  96. //find longest sequence in a row
  97.  
  98. for (int rows = 0; rows < matrix.GetLength(0); rows++)
  99. {
  100. for (int cols = 0; cols < matrix.GetLength(1); cols++)
  101. {
  102. int counter = 1;
  103.  
  104. int currentCol = cols + 1;
  105. if (currentCol == matrix.GetLength(1)) break; // if we are at the last element in the matrix stop;
  106.  
  107. if (matrix[rows, currentCol] == matrix[rows, cols])
  108. {
  109. while (currentCol < matrix.GetLength(1) && matrix[rows, currentCol] == matrix[rows, cols])
  110. {
  111. currentCol++;
  112. counter++;
  113. }
  114. }
  115. if (counter > maxCounter)
  116. {
  117. maxCounter = counter;
  118. result = matrix[rows, cols];
  119. }
  120. }
  121. }
  122.  
  123. // Console.WriteLine("{0} {1}", result, maxCounter);
  124.  
  125. //find the longest sequence in one diagonal (down, right)
  126.  
  127. for (int rows = 0; rows < matrix.GetLength(0); rows++)
  128. {
  129. for (int cols = 0; cols < matrix.GetLength(1); cols++)
  130. {
  131. int counter = 1;
  132.  
  133. int currentRow = rows + 1, currentCol = cols + 1;
  134.  
  135. if (currentRow == matrix.GetLength(0) || currentCol == matrix.GetLength(1)) break; //so we dont get IndexOutOFRange Ex.
  136.  
  137. if (matrix[currentRow, currentRow] == matrix[rows, cols])
  138. {
  139. while (currentRow < matrix.GetLength(0) && currentCol < matrix.GetLength(1)
  140. && matrix[currentRow, currentCol] == matrix[rows, cols])
  141. {
  142. currentRow++;
  143. currentCol++;
  144. counter++;
  145. }
  146. }
  147.  
  148. if (counter > maxCounter)
  149. {
  150. maxCounter = counter;
  151. result = matrix[rows, cols];
  152. }
  153. }
  154. }
  155.  
  156. //Console.WriteLine("{0} {1}", result, maxCounter);
  157.  
  158. //find longest sequence in a diagonal (down, left)
  159.  
  160. for (int rows = 0; rows < matrix.GetLength(0); rows ++)
  161. {
  162. for (int cols = matrix.GetLength(1) - 1; cols >= 0; cols--) // start from the leftmost element
  163. {
  164. int counter = 1;
  165.  
  166. int currentRow = rows + 1, currentCol = cols - 1;
  167.  
  168. if (currentRow == matrix.GetLength(0) || currentCol == 0) break; //IOOR exception skipping
  169.  
  170. if (matrix[currentRow, currentCol] == matrix[rows, cols])
  171. {
  172. while (currentRow < matrix.GetLength(0) && currentCol >= 0
  173. && matrix[currentRow, currentCol] == matrix[rows, cols]) // put the part with !!!!
  174. // indexes as a last logical statement
  175. { // else we get an IndexOutOfRange Exception
  176. currentRow ++;
  177. currentCol--;
  178. counter++;
  179. }
  180. }
  181.  
  182. if (counter > maxCounter)
  183. {
  184. maxCounter = counter;
  185. result = matrix[rows, cols];
  186. }
  187. }
  188. }
  189.  
  190. for(int i = 0; i < maxCounter; i ++)
  191. Console.Write("{0}, ", result);
  192. Console.WriteLine();
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement