Advertisement
Guest User

Untitled

a guest
Jul 9th, 2015
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.56 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 SequenceInMatrix
  8. {
  9.     class SequenceInMatrix
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             int m = int.Parse(Console.ReadLine());
  15.             string[,] matrix = new string[n, m];
  16.  
  17.             List<string> final = new List<string>();
  18.             for (int i = 0; i < matrix.GetLength(0); i++)
  19.             {
  20.                 for (int j = 0; j < matrix.GetLength(1); j++)
  21.                 {
  22.                     matrix[i, j] = Console.ReadLine();
  23.                 }
  24.             }
  25.             //horizontal
  26.             for (int i = 0; i < matrix.GetLength(0); i++)
  27.             {
  28.                 List<string> tempList = new List<string>();
  29.                 tempList.Add(matrix[i, 0]);
  30.                 for (int j = 1; j < matrix.GetLength(1); j++)
  31.                 {                  
  32.                     if (matrix[i,j] == matrix[i,j - 1])
  33.                     {
  34.                         tempList.Add(matrix[i,j]);
  35.                         if (j == matrix.GetLength(1) - 1)
  36.                         {
  37.                             if (tempList.Count > final.Count)
  38.                             {
  39.                                 final.Clear();
  40.                                 foreach (var item in tempList)
  41.                                 {
  42.                                     final.Add(item);
  43.                                 }
  44.                                 tempList.Clear();
  45.                                 tempList.Add(matrix[i, j]);
  46.                             }
  47.                             else
  48.                             {
  49.                                 tempList.Clear();
  50.                                 tempList.Add(matrix[i, j]);
  51.                             }
  52.                         }
  53.                     }
  54.                     else
  55.                     {
  56.                         if (tempList.Count > final.Count)
  57.                         {
  58.                             final.Clear();
  59.                             foreach (var item in tempList)
  60.                             {
  61.                                 final.Add(item);
  62.                             }
  63.                             tempList.Clear();
  64.                             tempList.Add(matrix[i,j]);
  65.                         }
  66.                         else
  67.                         {
  68.                             tempList.Clear();
  69.                             tempList.Add(matrix[i, j]);
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.             //vertical
  75.             for (int i = 0; i < matrix.GetLength(1); i++)
  76.             {
  77.                 List<string> tempList = new List<string>();
  78.                 tempList.Add(matrix[0, i]);
  79.                 for (int j = 1; j < matrix.GetLength(0); j++)
  80.                 {
  81.                     if (matrix[j, i] == matrix[j - 1, i])
  82.                     {
  83.                         tempList.Add(matrix[j, i]);
  84.                         if (j == matrix.GetLength(0) - 1)
  85.                         {
  86.                             if (tempList.Count > final.Count)
  87.                             {
  88.                                 final.Clear();
  89.                                 foreach (var item in tempList)
  90.                                 {
  91.                                     final.Add(item);
  92.                                 }
  93.                                 tempList.Clear();
  94.                                 tempList.Add(matrix[j, i]);
  95.                             }
  96.                             else
  97.                             {
  98.                                 tempList.Clear();
  99.                                 tempList.Add(matrix[j, i]);
  100.                             }
  101.                         }
  102.                     }
  103.                     else
  104.                     {
  105.                         if (tempList.Count > final.Count)
  106.                         {
  107.                             final.Clear();
  108.                             foreach (var item in tempList)
  109.                             {
  110.                                 final.Add(item);
  111.                             }
  112.                             tempList.Clear();
  113.                             tempList.Add(matrix[j, i]);
  114.                         }
  115.                         else
  116.                         {
  117.                             tempList.Clear();
  118.                             tempList.Add(matrix[j, i]);
  119.                         }
  120.                     }
  121.                 }
  122.             }
  123.             //diagonal
  124.             for (int i = 0; i < matrix.GetLength(1); i++)
  125.             {
  126.                 List<string> tempList = new List<string>();
  127.                 tempList.Add(matrix[0, i]);
  128.                 for (int j1 = 1, j2 = 1; j1 < matrix.GetLength(0); j1++, j2++)
  129.                 {
  130.                     if (matrix[j1, j2] == matrix[j1 - 1, j2 - 1])
  131.                     {
  132.                         tempList.Add(matrix[j1, j2]);
  133.                         if (j1 == matrix.GetLength(0) - 1)
  134.                         {
  135.                             if (tempList.Count > final.Count)
  136.                             {
  137.                                 final.Clear();
  138.                                 foreach (var item in tempList)
  139.                                 {
  140.                                     final.Add(item);
  141.                                 }
  142.                                 tempList.Clear();
  143.                                 tempList.Add(matrix[j1, j2]);
  144.                             }
  145.                             else
  146.                             {
  147.                                 tempList.Clear();
  148.                                 tempList.Add(matrix[j1, j2]);
  149.                             }
  150.                         }
  151.                     }
  152.                     else
  153.                     {
  154.                         if (tempList.Count > final.Count)
  155.                         {
  156.                             final.Clear();
  157.                             foreach (var item in tempList)
  158.                             {
  159.                                 final.Add(item);
  160.                             }
  161.                             tempList.Clear();
  162.                             tempList.Add(matrix[j1, j2]);
  163.                         }
  164.                         else
  165.                         {
  166.                             tempList.Clear();
  167.                             tempList.Add(matrix[j1, j2]);
  168.                         }
  169.                     }
  170.                 }
  171.             }
  172.             Console.WriteLine(String.Join(", ", final));
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement