Advertisement
dimipan80

Exam 7. Longest Alphabetical Word

Jul 19th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Threading;
  5.  
  6. public class LongestAlphabeticalWord
  7. {
  8.     public static void Main()
  9.     {
  10.         Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  11.         checked
  12.         {
  13.             string inputWord = Console.ReadLine();
  14.             int length = int.Parse(Console.ReadLine());
  15.  
  16.             string longestWord = inputWord[0].ToString();
  17.             if (length > 1)
  18.             {
  19.                 char[,] matrix = new char[length, length];
  20.                 int indexInString = 0;
  21.                 for (int row = 0; row < length; row++)
  22.                 {
  23.                     for (int col = 0; col < length; col++)
  24.                     {
  25.                         matrix[row, col] = inputWord[(indexInString % inputWord.Length)];
  26.                         indexInString++;
  27.                     }
  28.                 }
  29.  
  30.                 List<string> wordsList = new List<string>();
  31.                 for (int row = 0; row < length; row++)
  32.                 {
  33.                     for (int col = 0; col < length; col++)
  34.                     {
  35.                         string rightWord = FindAlphabeticalWord(matrix, length, row, col, 0, 1);
  36.                         wordsList.Add(rightWord);
  37.  
  38.                         string leftWord = FindAlphabeticalWord(matrix, length, row, col, 0, -1);
  39.                         wordsList.Add(leftWord);
  40.  
  41.                         string downWord = FindAlphabeticalWord(matrix, length, row, col, 1, 0);
  42.                         wordsList.Add(downWord);
  43.  
  44.                         string upperWord = FindAlphabeticalWord(matrix, length, row, col, -1, 0);
  45.                         wordsList.Add(upperWord);
  46.                     }
  47.                 }
  48.  
  49.                 wordsList.Sort();                
  50.                 int countLetters = 0;                
  51.                 foreach (string word in wordsList)
  52.                 {
  53.                     if (word.Length > countLetters)
  54.                     {
  55.                         countLetters = word.Length;
  56.                         longestWord = word;
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             Console.WriteLine(longestWord);            
  62.         }
  63.     }
  64.  
  65.     private static string FindAlphabeticalWord(char[,] matrix, int length, int row, int col, int directY, int directX)
  66.     {
  67.         checked
  68.         {
  69.             string word = string.Empty + matrix[row, col];
  70.             char previous = matrix[row, col];            
  71.             while (true)
  72.             {
  73.                 row += directY;
  74.                 col += directX;
  75.                 bool isInMatrix = row >= 0 && row < length && col >= 0 && col < length;
  76.                 if (!isInMatrix || matrix[row, col] <= previous)
  77.                 {
  78.                     break;
  79.                 }
  80.  
  81.                 word += matrix[row, col];
  82.                 previous = matrix[row, col];                
  83.             }            
  84.  
  85.             return word;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement