Guest User

Longest Alphabetical Word

a guest
Apr 18th, 2015
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.73 KB | None | 0 0
  1. /* Problem 4 – Longest Alphabetical Word
  2. Nakov enjoys playing with words. Recently he invented the following puzzle game. He starts by given word w (e.g. "softwareuniversity")
  3.  * and he fills a square block of size n*n (e.g. n=7) with this word as many times as it fits, from left to right and from up to down
  4.  * (see the example on the right). It is also called Nakov's square block of word w and size n.
  5. Nakov defines an alphabetical word as a sequence of letters, where each letter is alphabetically after its previous letter in the word. For example,
  6.  * "abc", "fo" and "aeou" are alphabetical words, but "zabc", "srevi" and "ntaeou" are not.
  7. Now Nakov wants to find the longest alphabetical word in the obtained square block. The word can start anywhere in the square block and can run in left,
  8.  * right, up or down direction and cannot go outside of the square block. In our example, if we start from row 3 and column 2 in our 7 x 7 square block,
  9.  * we find the following alphabetical words: "aw" (left direction), "ar" (right direction), "at" (up direction) and "aeou" (down direction).
  10. Write a program that reads a word w and a number n and finds the longest alphabetical word in Nakov's square block of word w and size n.
  11.  * If more than one longest alphabetical words exist in the block, find the smallest of them in the standard lexicographical order.
  12. Input
  13. The input data should be read from the console. The input data consists of exactly two lines:
  14. • The first line will hold the word w.
  15. • The second line will hold the size n.
  16. The input data will always be valid and in the format described. There is no need to check it explicitly.
  17. Output
  18. You have to print at the console the longest alphabetical word.
  19. Constraints
  20. • The word w will be a non-empty string, consisting of lower Latin letters, up 1000.
  21. • The size of the square n will be an integer value in the range [1…50].
  22.  */
  23.  
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Text;
  27. using System.Linq;
  28.  
  29. class LongestAlphabeticalWord
  30. {
  31.     static void Main()
  32.     {
  33.         // input
  34.         char[] w = Console.ReadLine().ToCharArray();
  35.         int n = int.Parse(Console.ReadLine());
  36.  
  37.         // declarations
  38.         int length = w.Length;
  39.         int index = 0;
  40.         char[,] square = new char[n, n];
  41.         int count = 1;
  42.         int maxSecq = 1;
  43.         StringBuilder maxValue = new StringBuilder();
  44.         Dictionary<string, int> results = new Dictionary<string, int>();
  45.  
  46.         // filling the square (the matrix) with the letters from the word
  47.         for (int row = 0; row < n; row++)
  48.         {
  49.             for (int col = 0; col < n; col++)
  50.             {
  51.                 square[row, col] = w[index % length];
  52.                 index++;
  53.             }
  54.         }
  55.  
  56.         // if n = 1, we just print the whole matrix representing 1 single letter
  57.         if (n == 1)
  58.         {
  59.             for (int row = 0; row < n; row++)
  60.             {
  61.                 for (int col = 0; col < n; col++)
  62.                 {
  63.                     Console.Write(square[row, col]);
  64.                 }
  65.                 Console.WriteLine();
  66.             }
  67.             return;
  68.         }
  69.  
  70.         // in case you want to see how the entire square looks like
  71.         //for (int row = 0; row < n; row++)
  72.         //{
  73.         //    for (int col = 0; col < n; col++)
  74.         //    {
  75.         //        Console.Write(square[row, col]);
  76.         //    }
  77.         //    Console.WriteLine();
  78.         //}
  79.  
  80.         //Searching horizontally, from left to right, for increasing values
  81.         for (int row = 0; row < square.GetLength(0); row++)
  82.         {
  83.             for (int col = 0; col < square.GetLength(1) - 1; col++)    
  84.             {
  85.                 maxValue.Append(square[row, col]);
  86.                 //Console.WriteLine(square[row, col]);
  87.                 if ((square[row, col] < square[row, col + 1]))
  88.                 {
  89.                     count++;
  90.  
  91.                     maxValue.Append(square[row, col + 1]);
  92.                 }
  93.  
  94.                 else
  95.                 {
  96.                     count = 1;
  97.                     maxValue.Clear();
  98.                 }
  99.                 if (count >= maxSecq)
  100.                 {
  101.                     maxSecq = count;
  102.                     string temp = new string(maxValue.ToString().ToCharArray().Distinct().ToArray());
  103.                     if (!results.ContainsKey(temp))
  104.                     {
  105.                         results.Add(temp, count);
  106.                     }
  107.                 }
  108.  
  109.             }
  110.             count = 1;
  111.             maxValue.Clear();
  112.         }
  113.  
  114.         //Searching horizontally, from left to right, for decreasing values
  115.         for (int row = 0; row < square.GetLength(0); row++)
  116.         {
  117.             for (int col = 0; col < square.GetLength(1) - 1; col++)    
  118.             {
  119.                 maxValue.Append(square[row, col]);
  120.                 if ((square[row, col] > square[row, col + 1]))
  121.                 {
  122.                     count++;
  123.                     maxValue.Append(square[row, col + 1]);
  124.                 }
  125.  
  126.                 else
  127.                 {
  128.                     count = 1;
  129.                     maxValue.Clear();
  130.                 }
  131.                 if (count >= maxSecq)
  132.                 {
  133.                     maxSecq = count;
  134.                     // reversing the string (as if we have searched from right to left)
  135.                     string temp = new string(maxValue.ToString().ToCharArray().Distinct().Reverse().ToArray());
  136.                     if (!results.ContainsKey(temp))
  137.                     {
  138.                         results.Add(temp, count);
  139.                     }
  140.                 }
  141.  
  142.             }
  143.             count = 1;
  144.             maxValue.Clear();
  145.         }
  146.  
  147.         //Searching vertically, top to bottom, for decreasing values
  148.         for (int col = 0; col < square.GetLength(1); col++)                
  149.         {
  150.             for (int row = 0; row < square.GetLength(0) - 1; row++)
  151.             {
  152.                 maxValue.Append(square[row, col]);
  153.                 if ((square[row, col] < square[row + 1, col]))
  154.                 {
  155.                     count++;
  156.                     maxValue.Append(square[row + 1, col]);
  157.                 }
  158.                 else
  159.                 {
  160.                     count = 1;
  161.                     maxValue.Clear();
  162.                 }
  163.                 if (count >= maxSecq)
  164.                 {
  165.                     maxSecq = count;
  166.                     string temp = new string(maxValue.ToString().ToCharArray().Distinct().ToArray());
  167.                     if (!results.ContainsKey(temp))
  168.                     {
  169.                         results.Add(temp, count);
  170.                     }
  171.  
  172.                 }
  173.             }
  174.             count = 1;
  175.             maxValue.Clear();
  176.         }
  177.  
  178.         //Searching vertically, top to bottom, for decreasing values
  179.         for (int col = 0; col < square.GetLength(1); col++)                
  180.         {
  181.             for (int row = 0; row < square.GetLength(0) - 1; row++)
  182.             {
  183.                 maxValue.Append(square[row, col]);
  184.                 if ((square[row, col] > square[row + 1, col]))
  185.                 {
  186.                     count++;
  187.                     maxValue.Append(square[row + 1, col]);
  188.                 }
  189.                 else
  190.                 {
  191.                     count = 1;
  192.                     maxValue.Clear();
  193.                 }
  194.                 if (count >= maxSecq)
  195.                 {
  196.                     maxSecq = count;
  197.                     // reversing the string (as if we have searched bottom to top)
  198.                     string temp = new string(maxValue.ToString().ToCharArray().Distinct().Reverse().ToArray());
  199.                     if (!results.ContainsKey(temp))
  200.                     {
  201.                         results.Add(temp, count);
  202.                     }
  203.  
  204.                 }
  205.             }
  206.             count = 1;
  207.             maxValue.Clear();
  208.         }
  209.  
  210.         // removing empty key entries
  211.         results.Remove(String.Empty);
  212.  
  213.         // these would be all strings with max length
  214.         int max = results.Values.Max();
  215.         List<string> final = new List<string>();
  216.         foreach (KeyValuePair<string, int> pair in results)
  217.         {
  218.             if (pair.Value  == max)
  219.             {
  220.                 // adding to final all strings with max length
  221.                 final.Add(pair.Key);
  222.             }
  223.         }
  224.  
  225.         // "If more than one longest alphabetical words exist in the block, find the smallest of them in the standard lexicographical order"
  226.         Console.WriteLine(final.Min());
  227.  
  228.         // in case you want to see all strings
  229.         //foreach (KeyValuePair<string, int> pair in results)
  230.         //{
  231.         //    Console.WriteLine("{0} {1}", pair.Key, pair.Value);
  232.         //}
  233.  
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment