Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class LongestAlphabeticalWord
- {
- static void Main(string[] args)
- {
- string word = Console.ReadLine();
- int matrixSize = int.Parse(Console.ReadLine());
- string[,] matrix = new string[matrixSize, matrixSize];
- int wordCounter = 0;
- for (int rows = 0; rows < matrix.GetLength(0); rows++)
- {
- for (int cols = 0; cols < matrix.GetLength(1); cols++)
- {
- matrix[rows, cols] = word[wordCounter].ToString();
- wordCounter++;
- if (wordCounter == word.Length)
- {
- wordCounter = 0;
- }
- }
- }
- List<string> longestWord = new List<string>();
- // Console.WriteLine("b".CompareTo("a"));
- for (int rows = 0; rows < matrix.GetLength(0); rows++)
- {
- for (int cols = 0; cols < matrix.GetLength(1); cols++)
- {
- string tempSequence = string.Empty;
- tempSequence = Left(matrix, rows, cols);
- IsEqualOrLonger(longestWord, tempSequence);
- tempSequence = Right(matrix, rows, cols);
- IsEqualOrLonger(longestWord, tempSequence);
- tempSequence = UP(matrix, rows, cols);
- IsEqualOrLonger(longestWord, tempSequence);
- tempSequence = Down(matrix, rows, cols);
- IsEqualOrLonger(longestWord, tempSequence);
- }
- }
- longestWord.Sort();
- Console.WriteLine(longestWord[0]);
- }
- private static string Left(string[,] matrix, int rows, int cols)
- {
- string tempSequence = matrix[rows, cols];
- int nextIndex = cols - 1;
- while (nextIndex >= 0)
- {
- string thisChar = tempSequence[tempSequence.Length - 1].ToString();
- string nextChar = matrix[rows, nextIndex];
- if (thisChar.CompareTo(nextChar) == -1)
- {
- tempSequence += nextChar;
- nextIndex--;
- }
- else
- {
- break;
- }
- }
- return tempSequence;
- }
- private static string Right(string[,] matrix, int rows, int cols)
- {
- string tempSequence = matrix[rows, cols];
- int nextIndex = cols + 1;
- while (nextIndex < matrix.GetLength(1))
- {
- string thisChar = tempSequence[tempSequence.Length - 1].ToString();
- string nextChar = matrix[rows, nextIndex];
- if (thisChar.CompareTo(nextChar) == -1)
- {
- tempSequence += nextChar;
- nextIndex++;
- }
- else
- {
- break;
- }
- }
- return tempSequence;
- }
- private static string UP(string[,] matrix, int rows, int cols)
- {
- string tempSequence = matrix[rows, cols];
- int nextIndex = rows - 1;
- while (nextIndex >= 0)
- {
- string thisChar = tempSequence[tempSequence.Length - 1].ToString();
- string nextChar = matrix[nextIndex, cols];
- if (thisChar.CompareTo(nextChar) == -1)
- {
- tempSequence += nextChar;
- nextIndex--;
- }
- else
- {
- break;
- }
- }
- return tempSequence;
- }
- private static string Down(string[,] matrix, int rows, int cols)
- {
- string tempSequence = matrix[rows, cols];
- int nextIndex = rows + 1;
- while (nextIndex < matrix.GetLength(0))
- {
- string thisChar = tempSequence[tempSequence.Length - 1].ToString();
- string nextChar = matrix[nextIndex, cols];
- if (thisChar.CompareTo(nextChar) == -1)
- {
- tempSequence += nextChar;
- nextIndex++;
- }
- else
- {
- break;
- }
- }
- return tempSequence;
- }
- private static void IsEqualOrLonger(List<string> longestWord, string tempSequence)
- {
- if ((longestWord.Count > 0 && tempSequence.Length >= longestWord[0].Length) || longestWord.Count == 0)
- {
- IsStringLonger(longestWord, tempSequence);
- }
- }
- private static void IsStringLonger(List<string> longestAlphabeticalWord, string ch)
- {
- if (longestAlphabeticalWord.Count == 0)
- {
- longestAlphabeticalWord.Add(ch);
- }
- else if (ch.Length >= longestAlphabeticalWord[0].Length)
- {
- if (ch.Length == longestAlphabeticalWord[0].Length)
- {
- longestAlphabeticalWord.Add(ch);
- }
- else
- {
- longestAlphabeticalWord.Clear();
- longestAlphabeticalWord.Add(ch);
- }
- }
- }
- private static void PrintMatrix(string[,] matrix)
- {
- for (int rows = 0; rows < matrix.GetLength(0); rows++)
- {
- for (int cols = 0; cols < matrix.GetLength(1); cols++)
- {
- Console.Write(matrix[rows, cols]);
- }
- Console.WriteLine();
- }
- }
- }
- /*
- softwareuniversity
- 7
- alpha
- 6
- java
- 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement