Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Threading;
- public class LongestAlphabeticalWord
- {
- public static void Main()
- {
- Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
- checked
- {
- string inputWord = Console.ReadLine();
- int length = int.Parse(Console.ReadLine());
- string longestWord = inputWord[0].ToString();
- if (length > 1)
- {
- char[,] matrix = new char[length, length];
- int indexInString = 0;
- for (int row = 0; row < length; row++)
- {
- for (int col = 0; col < length; col++)
- {
- matrix[row, col] = inputWord[(indexInString % inputWord.Length)];
- indexInString++;
- }
- }
- List<string> wordsList = new List<string>();
- for (int row = 0; row < length; row++)
- {
- for (int col = 0; col < length; col++)
- {
- string rightWord = FindAlphabeticalWord(matrix, length, row, col, 0, 1);
- wordsList.Add(rightWord);
- string leftWord = FindAlphabeticalWord(matrix, length, row, col, 0, -1);
- wordsList.Add(leftWord);
- string downWord = FindAlphabeticalWord(matrix, length, row, col, 1, 0);
- wordsList.Add(downWord);
- string upperWord = FindAlphabeticalWord(matrix, length, row, col, -1, 0);
- wordsList.Add(upperWord);
- }
- }
- wordsList.Sort();
- int countLetters = 0;
- foreach (string word in wordsList)
- {
- if (word.Length > countLetters)
- {
- countLetters = word.Length;
- longestWord = word;
- }
- }
- }
- Console.WriteLine(longestWord);
- }
- }
- private static string FindAlphabeticalWord(char[,] matrix, int length, int row, int col, int directY, int directX)
- {
- checked
- {
- string word = string.Empty + matrix[row, col];
- char previous = matrix[row, col];
- while (true)
- {
- row += directY;
- col += directX;
- bool isInMatrix = row >= 0 && row < length && col >= 0 && col < length;
- if (!isInMatrix || matrix[row, col] <= previous)
- {
- break;
- }
- word += matrix[row, col];
- previous = matrix[row, col];
- }
- return word;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement