Advertisement
GogoK

Longest Alphabetical Word

Dec 16th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class LongestAlphabeticalWord
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         string word = Console.ReadLine();
  9.         int matrixSize = int.Parse(Console.ReadLine());
  10.  
  11.         string[,] matrix = new string[matrixSize, matrixSize];
  12.  
  13.         int wordCounter = 0;
  14.  
  15.         for (int rows = 0; rows < matrix.GetLength(0); rows++)
  16.         {
  17.             for (int cols = 0; cols < matrix.GetLength(1); cols++)
  18.             {
  19.                 matrix[rows, cols] = word[wordCounter].ToString();
  20.                 wordCounter++;
  21.                 if (wordCounter == word.Length)
  22.                 {
  23.                     wordCounter = 0;
  24.                 }
  25.             }
  26.         }
  27.  
  28.         List<string> longestWord = new List<string>();
  29.  
  30.         // Console.WriteLine("b".CompareTo("a"));
  31.  
  32.         for (int rows = 0; rows < matrix.GetLength(0); rows++)
  33.         {
  34.             for (int cols = 0; cols < matrix.GetLength(1); cols++)
  35.             {
  36.                 string tempSequence = string.Empty;
  37.  
  38.                 tempSequence = Left(matrix, rows, cols);
  39.                 IsEqualOrLonger(longestWord, tempSequence);
  40.                
  41.                 tempSequence = Right(matrix, rows, cols);
  42.                 IsEqualOrLonger(longestWord, tempSequence);
  43.  
  44.                 tempSequence = UP(matrix, rows, cols);
  45.                 IsEqualOrLonger(longestWord, tempSequence);
  46.  
  47.                 tempSequence = Down(matrix, rows, cols);
  48.                 IsEqualOrLonger(longestWord, tempSequence);
  49.             }
  50.         }
  51.        
  52.         longestWord.Sort();
  53.         Console.WriteLine(longestWord[0]);
  54.     }
  55.  
  56.     private static string Left(string[,] matrix, int rows, int cols)
  57.     {
  58.         string tempSequence = matrix[rows, cols];
  59.  
  60.         int nextIndex = cols - 1;
  61.         while (nextIndex >= 0)
  62.         {
  63.             string thisChar = tempSequence[tempSequence.Length - 1].ToString();
  64.             string nextChar = matrix[rows, nextIndex];
  65.  
  66.             if (thisChar.CompareTo(nextChar) == -1)
  67.             {
  68.                 tempSequence += nextChar;
  69.                 nextIndex--;
  70.             }
  71.             else
  72.             {
  73.                 break;
  74.             }
  75.         }
  76.         return tempSequence;
  77.     }
  78.  
  79.     private static string Right(string[,] matrix, int rows, int cols)
  80.     {
  81.         string tempSequence = matrix[rows, cols];
  82.  
  83.         int nextIndex = cols + 1;
  84.         while (nextIndex < matrix.GetLength(1))
  85.         {
  86.             string thisChar = tempSequence[tempSequence.Length - 1].ToString();
  87.             string nextChar = matrix[rows, nextIndex];
  88.  
  89.             if (thisChar.CompareTo(nextChar) == -1)
  90.             {
  91.                 tempSequence += nextChar;
  92.                 nextIndex++;
  93.             }
  94.             else
  95.             {
  96.                 break;
  97.             }
  98.         }
  99.         return tempSequence;
  100.     }
  101.  
  102.     private static string UP(string[,] matrix, int rows, int cols)
  103.     {
  104.         string tempSequence = matrix[rows, cols];
  105.  
  106.         int nextIndex = rows - 1;
  107.         while (nextIndex >= 0)
  108.         {
  109.             string thisChar = tempSequence[tempSequence.Length - 1].ToString();
  110.             string nextChar = matrix[nextIndex, cols];
  111.  
  112.             if (thisChar.CompareTo(nextChar) == -1)
  113.             {
  114.                 tempSequence += nextChar;
  115.                 nextIndex--;
  116.             }
  117.             else
  118.             {
  119.                 break;
  120.             }
  121.         }
  122.         return tempSequence;
  123.     }
  124.  
  125.     private static string Down(string[,] matrix, int rows, int cols)
  126.     {
  127.         string tempSequence = matrix[rows, cols];
  128.  
  129.         int nextIndex = rows + 1;
  130.         while (nextIndex < matrix.GetLength(0))
  131.         {
  132.             string thisChar = tempSequence[tempSequence.Length - 1].ToString();
  133.             string nextChar = matrix[nextIndex, cols];
  134.  
  135.             if (thisChar.CompareTo(nextChar) == -1)
  136.             {
  137.                 tempSequence += nextChar;
  138.                 nextIndex++;
  139.             }
  140.             else
  141.             {
  142.                 break;
  143.             }
  144.         }
  145.         return tempSequence;
  146.     }
  147.  
  148.     private static void IsEqualOrLonger(List<string> longestWord, string tempSequence)
  149.     {
  150.         if ((longestWord.Count > 0 && tempSequence.Length >= longestWord[0].Length) || longestWord.Count == 0)
  151.         {
  152.             IsStringLonger(longestWord, tempSequence);
  153.         }
  154.     }
  155.  
  156.     private static void IsStringLonger(List<string> longestAlphabeticalWord, string ch)
  157.     {
  158.         if (longestAlphabeticalWord.Count == 0)
  159.         {
  160.             longestAlphabeticalWord.Add(ch);
  161.         }
  162.         else if (ch.Length >= longestAlphabeticalWord[0].Length)
  163.         {
  164.             if (ch.Length == longestAlphabeticalWord[0].Length)
  165.             {
  166.                 longestAlphabeticalWord.Add(ch);
  167.             }
  168.             else
  169.             {
  170.                 longestAlphabeticalWord.Clear();
  171.                 longestAlphabeticalWord.Add(ch);
  172.             }
  173.         }
  174.     }
  175.  
  176.     private static void PrintMatrix(string[,] matrix)
  177.     {
  178.         for (int rows = 0; rows < matrix.GetLength(0); rows++)
  179.         {
  180.             for (int cols = 0; cols < matrix.GetLength(1); cols++)
  181.             {
  182.                 Console.Write(matrix[rows, cols]);
  183.             }
  184.             Console.WriteLine();
  185.         }
  186.     }
  187. }
  188.  
  189.  
  190.  
  191. /*
  192.  
  193. softwareuniversity
  194. 7
  195.  
  196. alpha
  197. 6
  198.  
  199. java
  200. 3
  201. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement