Advertisement
fbinnzhivko

04.00 Longest Alphabetical Word

Apr 17th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5.  
  6. class LongestAlphabeticalWord
  7. {
  8.     static void Main()
  9.     {
  10.         char[] w = Console.ReadLine().ToCharArray();
  11.         int n = int.Parse(Console.ReadLine());
  12.        
  13.         int length = w.Length;
  14.         int index = 0;
  15.         char[,] square = new char[n, n];
  16.         int count = 1;
  17.         int maxSecq = 1;
  18.         StringBuilder maxValue = new StringBuilder();
  19.         Dictionary<string, int> results = new Dictionary<string, int>();
  20.  
  21.         // filling the square (the matrix) with the letters from the word
  22.         for (int row = 0; row < n; row++)
  23.         {
  24.             for (int col = 0; col < n; col++)
  25.             {
  26.                 square[row, col] = w[index % length];
  27.                 index++;
  28.             }
  29.         }
  30.  
  31.         // if n = 1, we just print the whole matrix representing 1 single letter
  32.         if (n == 1)
  33.         {
  34.             for (int row = 0; row < n; row++)
  35.             {
  36.                 for (int col = 0; col < n; col++)
  37.                 {
  38.                     Console.Write(square[row, col]);
  39.                 }
  40.                 Console.WriteLine();
  41.             }
  42.             return;
  43.         }
  44.  
  45.         // in case you want to see how the entire square looks like
  46.         //for (int row = 0; row < n; row++)
  47.         //{
  48.         //    for (int col = 0; col < n; col++)
  49.         //    {
  50.         //        Console.Write(square[row, col]);
  51.         //    }
  52.         //    Console.WriteLine();
  53.         //}
  54.  
  55.         //Searching horizontally, from left to right, for increasing values
  56.         for (int row = 0; row < square.GetLength(0); row++)
  57.         {
  58.             for (int col = 0; col < square.GetLength(1) - 1; col++)
  59.             {
  60.                 maxValue.Append(square[row, col]);
  61.                 //Console.WriteLine(square[row, col]);
  62.                 if ((square[row, col] < square[row, col + 1]))
  63.                 {
  64.                     count++;
  65.  
  66.                     maxValue.Append(square[row, col + 1]);
  67.                 }
  68.  
  69.                 else
  70.                 {
  71.                     count = 1;
  72.                     maxValue.Clear();
  73.                 }
  74.                 if (count >= maxSecq)
  75.                 {
  76.                     maxSecq = count;
  77.                     string temp = new string(maxValue.ToString().ToCharArray().Distinct().ToArray());
  78.                     if (!results.ContainsKey(temp))
  79.                     {
  80.                         results.Add(temp, count);
  81.                     }
  82.                 }
  83.  
  84.             }
  85.             count = 1;
  86.             maxValue.Clear();
  87.         }
  88.  
  89.         //Searching horizontally, from left to right, for decreasing values
  90.         for (int row = 0; row < square.GetLength(0); row++)
  91.         {
  92.             for (int col = 0; col < square.GetLength(1) - 1; col++)
  93.             {
  94.                 maxValue.Append(square[row, col]);
  95.                 if ((square[row, col] > square[row, col + 1]))
  96.                 {
  97.                     count++;
  98.                     maxValue.Append(square[row, col + 1]);
  99.                 }
  100.  
  101.                 else
  102.                 {
  103.                     count = 1;
  104.                     maxValue.Clear();
  105.                 }
  106.                 if (count >= maxSecq)
  107.                 {
  108.                     maxSecq = count;
  109.                     // reversing the string (as if we have searched from right to left)
  110.                     string temp = new string(maxValue.ToString().ToCharArray().Distinct().Reverse().ToArray());
  111.                     if (!results.ContainsKey(temp))
  112.                     {
  113.                         results.Add(temp, count);
  114.                     }
  115.                 }
  116.  
  117.             }
  118.             count = 1;
  119.             maxValue.Clear();
  120.         }
  121.  
  122.         //Searching vertically, top to bottom, for decreasing values
  123.         for (int col = 0; col < square.GetLength(1); col++)
  124.         {
  125.             for (int row = 0; row < square.GetLength(0) - 1; row++)
  126.             {
  127.                 maxValue.Append(square[row, col]);
  128.                 if ((square[row, col] < square[row + 1, col]))
  129.                 {
  130.                     count++;
  131.                     maxValue.Append(square[row + 1, col]);
  132.                 }
  133.                 else
  134.                 {
  135.                     count = 1;
  136.                     maxValue.Clear();
  137.                 }
  138.                 if (count >= maxSecq)
  139.                 {
  140.                     maxSecq = count;
  141.                     string temp = new string(maxValue.ToString().ToCharArray().Distinct().ToArray());
  142.                     if (!results.ContainsKey(temp))
  143.                     {
  144.                         results.Add(temp, count);
  145.                     }
  146.  
  147.                 }
  148.             }
  149.             count = 1;
  150.             maxValue.Clear();
  151.         }
  152.  
  153.         //Searching vertically, top to bottom, for decreasing values
  154.         for (int col = 0; col < square.GetLength(1); col++)
  155.         {
  156.             for (int row = 0; row < square.GetLength(0) - 1; row++)
  157.             {
  158.                 maxValue.Append(square[row, col]);
  159.                 if ((square[row, col] > square[row + 1, col]))
  160.                 {
  161.                     count++;
  162.                     maxValue.Append(square[row + 1, col]);
  163.                 }
  164.                 else
  165.                 {
  166.                     count = 1;
  167.                     maxValue.Clear();
  168.                 }
  169.                 if (count >= maxSecq)
  170.                 {
  171.                     maxSecq = count;
  172.                     // reversing the string (as if we have searched bottom to top)
  173.                     string temp = new string(maxValue.ToString().ToCharArray().Distinct().Reverse().ToArray());
  174.                     if (!results.ContainsKey(temp))
  175.                     {
  176.                         results.Add(temp, count);
  177.                     }
  178.  
  179.                 }
  180.             }
  181.             count = 1;
  182.             maxValue.Clear();
  183.         }
  184.  
  185.         // removing empty key entries
  186.         results.Remove(String.Empty);
  187.  
  188.         // these would be all strings with max length
  189.         int max = results.Values.Max();
  190.         List<string> final = new List<string>();
  191.         foreach (KeyValuePair<string, int> pair in results)
  192.         {
  193.             if (pair.Value == max)
  194.             {
  195.                 // adding to final all strings with max length
  196.                 final.Add(pair.Key);
  197.             }
  198.         }
  199.  
  200.         // "If more than one longest alphabetical words exist in the block, find the smallest of them in the standard lexicographical order"
  201.         Console.WriteLine(final.Min());
  202.  
  203.         // in case you want to see all strings
  204.         //foreach (KeyValuePair<string, int> pair in results)
  205.         //{
  206.         //    Console.WriteLine("{0} {1}", pair.Key, pair.Value);
  207.         //}
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement