svetlozar_kirkov

Longest Alphabetical Word

Dec 14th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace LongestAlphabeticalWord
  6. {
  7.     class LongestAlphabeticalWord
  8.     {
  9.         static void Main()
  10.         {
  11.             string word = Console.ReadLine();
  12.             int size = int.Parse(Console.ReadLine());
  13.             if (size == 1)
  14.             {
  15.                 Console.WriteLine(word[0]);
  16.                 return;
  17.             }
  18.             char[,] block = new char[size,size];
  19.             string longest = "";
  20.             char currentLetter = word[0];
  21.             int index = 0;
  22.             List<string> collected = new List<string>();
  23.  
  24.             for (int i = 0; i < size; i++)
  25.             {
  26.                 for (int j = 0; j < size; j++)
  27.                 {
  28.                     if (index == word.Length-1)
  29.                     {
  30.                         block[i, j] = currentLetter;
  31.                         index = 0;
  32.                         currentLetter = word[index];
  33.                     }
  34.                     else
  35.                     {
  36.                         block[i, j] = currentLetter;
  37.                         currentLetter = word[index + 1];
  38.                         index++;
  39.                     }
  40.                 }
  41.             }
  42.             for (int x = 0; x < 4; x++)
  43.             {
  44.                 for (int i = 0; i < size; i++)
  45.                 {
  46.                     StringBuilder temp = new StringBuilder();
  47.                     temp.Append(block[i, 0]);
  48.                     char prev = block[i, 0];
  49.  
  50.                     for (int j = 1; j < size; j++)
  51.                     {
  52.                         if ((int)block[i, j] > (int)prev)
  53.                         {
  54.                             temp.Append(block[i, j]);
  55.                             prev = block[i, j];
  56.                             longest = temp.ToString();
  57.                             collected.Add(temp.ToString());
  58.                         }
  59.                         else
  60.                         {
  61.                             temp.Clear();
  62.                             temp.Append(block[i, j]);
  63.                             prev = block[i, j];
  64.                         }
  65.                     }
  66.                 }
  67.                 block = RotateMatrix(block, size);
  68.             }
  69.             foreach (var alpha in collected)
  70.             {
  71.                 if (alpha.Length>longest.Length)
  72.                 {
  73.                     longest = alpha;
  74.                 }
  75.             }
  76.             List<string> longestCollection = new List<string>();
  77.             foreach (var alpha in collected)
  78.             {
  79.                 if (alpha.Length==longest.Length)
  80.                 {
  81.                     longestCollection.Add(alpha);
  82.                 }
  83.             }
  84.             foreach (var temp in longestCollection)
  85.             {
  86.                 if (string.CompareOrdinal(temp,longest)<0)
  87.                 {
  88.                     longest = temp;
  89.                 }
  90.             }
  91.             Console.WriteLine(longest);
  92.         }
  93.         static char[,] RotateMatrix(char[,] matrix, int n)
  94.         {
  95.             char[,] ret = new char[n, n];
  96.  
  97.             for (int i = 0; i < n; ++i)
  98.             {
  99.                 for (int j = 0; j < n; ++j)
  100.                 {
  101.                     ret[i, j] = matrix[n - j - 1, i];
  102.                 }
  103.             }
  104.             return ret;
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment