Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace LongestAlphabeticalWord
- {
- class LongestAlphabeticalWord
- {
- static void Main()
- {
- string word = Console.ReadLine();
- int size = int.Parse(Console.ReadLine());
- if (size == 1)
- {
- Console.WriteLine(word[0]);
- return;
- }
- char[,] block = new char[size,size];
- string longest = "";
- char currentLetter = word[0];
- int index = 0;
- List<string> collected = new List<string>();
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- if (index == word.Length-1)
- {
- block[i, j] = currentLetter;
- index = 0;
- currentLetter = word[index];
- }
- else
- {
- block[i, j] = currentLetter;
- currentLetter = word[index + 1];
- index++;
- }
- }
- }
- for (int x = 0; x < 4; x++)
- {
- for (int i = 0; i < size; i++)
- {
- StringBuilder temp = new StringBuilder();
- temp.Append(block[i, 0]);
- char prev = block[i, 0];
- for (int j = 1; j < size; j++)
- {
- if ((int)block[i, j] > (int)prev)
- {
- temp.Append(block[i, j]);
- prev = block[i, j];
- longest = temp.ToString();
- collected.Add(temp.ToString());
- }
- else
- {
- temp.Clear();
- temp.Append(block[i, j]);
- prev = block[i, j];
- }
- }
- }
- block = RotateMatrix(block, size);
- }
- foreach (var alpha in collected)
- {
- if (alpha.Length>longest.Length)
- {
- longest = alpha;
- }
- }
- List<string> longestCollection = new List<string>();
- foreach (var alpha in collected)
- {
- if (alpha.Length==longest.Length)
- {
- longestCollection.Add(alpha);
- }
- }
- foreach (var temp in longestCollection)
- {
- if (string.CompareOrdinal(temp,longest)<0)
- {
- longest = temp;
- }
- }
- Console.WriteLine(longest);
- }
- static char[,] RotateMatrix(char[,] matrix, int n)
- {
- char[,] ret = new char[n, n];
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- ret[i, j] = matrix[n - j - 1, i];
- }
- }
- return ret;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment