Advertisement
Stephen_MS

Ex. Longest Alphabetical Word - word in a matrix with size n

Dec 16th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class LongestAlphabeticalWord
  5. {
  6.     static void Main()
  7.     {
  8.         string inputWord = Console.ReadLine();
  9.         int n = int.Parse(Console.ReadLine());
  10.  
  11.         //// Print the matrix of letters
  12.         //for (int row = 0; row < n; row++)
  13.         //{
  14.         //    for (int col = 0; col < n; col++)
  15.         //    {
  16.         //        Console.Write(GetLetterAtPosition(inputWord, n, row, col));
  17.         //    }
  18.         //    Console.WriteLine();
  19.         //}
  20.  
  21.         // Find the longest alphabetical word in the matrix of letters
  22.         string longestAlphaWord = "";
  23.         for (int row = 0; row < n; row++)
  24.         {
  25.             for (int col = 0; col < n; col++)
  26.             {
  27.                 string strLeft = FindAlphabeticalWord(inputWord, n, row, col, -1, 0);
  28.                 longestAlphaWord = GetBetterWord(longestAlphaWord, strLeft);
  29.                 string strRight = FindAlphabeticalWord(inputWord, n, row, col, +1, 0);
  30.                 longestAlphaWord = GetBetterWord(longestAlphaWord, strRight);
  31.                 string strUp = FindAlphabeticalWord(inputWord, n, row, col, 0, -1);
  32.                 longestAlphaWord = GetBetterWord(longestAlphaWord, strUp);
  33.                 string strDown = FindAlphabeticalWord(inputWord, n, row, col, 0, +1);
  34.                 longestAlphaWord = GetBetterWord(longestAlphaWord, strDown);
  35.             }
  36.         }
  37.  
  38.         Console.WriteLine(longestAlphaWord);
  39.     }
  40.  
  41.     private static string FindAlphabeticalWord(
  42.         string inputWord, int n, int row, int col, int directionX, int directionY)
  43.     {
  44.         List<char> letters = new List<char>();
  45.         char previousLetter = GetLetterAtPosition(inputWord, n, row, col);
  46.         letters.Add(previousLetter);
  47.         while (true)
  48.         {
  49.             col = col + directionX;
  50.             row = row + directionY;
  51.             if (row < 0 || row >= n || col < 0 || col >= n)
  52.             {
  53.                 // We are of range --> stop appending letters
  54.                 break;
  55.             }
  56.             char nextLetter = GetLetterAtPosition(inputWord, n, row, col);
  57.             if (nextLetter <= previousLetter)
  58.             {
  59.                 // Non alphabetical order --> stop appending letters
  60.                 break;
  61.             }
  62.             letters.Add(nextLetter);
  63.             previousLetter = nextLetter;
  64.         }
  65.  
  66.         string alphaWord = new string(letters.ToArray());
  67.         return alphaWord;
  68.     }
  69.  
  70.     private static char GetLetterAtPosition(string inputWord, int n, int row, int col)
  71.     {
  72.         char ch = inputWord[(row * n + col) % inputWord.Length];
  73.         return ch;
  74.     }
  75.  
  76.     private static string GetBetterWord(string firstWord, string secondWord)
  77.     {
  78.         if ((firstWord.Length > secondWord.Length) ||
  79.             ((firstWord.Length == secondWord.Length &&
  80.              firstWord.CompareTo(secondWord) < 0)))
  81.         {
  82.             return firstWord;
  83.         }
  84.  
  85.         return secondWord;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement