aleahim

LongestAlphabeticalWord_14.04.2014_SoftUni

Apr 15th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class LongestAlphabeticalWord
  8. {
  9.     static void Main()
  10.     {
  11.         string w = Console.ReadLine();
  12.         int n = int.Parse(Console.ReadLine());
  13.  
  14.         char[] letters = w.ToCharArray();
  15.  
  16.         char[][] block = new char[n][];
  17.         int k = 0;
  18.         for (int i = 0; i < n; i++)
  19.         {
  20.             block[i] = new char[n]; // Create inner array
  21.             for (int j = 0; j < n; j++)
  22.             {
  23.                 if (k == letters.Length)
  24.                 {
  25.                     k = 0;
  26.                 }
  27.                 block[i][j] = letters[k];
  28.                 k++;
  29.             }
  30.         }
  31.  
  32.         List<string> all = new List<string>();      
  33.         string word = "";
  34.  
  35.         char next;
  36.            for (int i = 0; i < n; i++)
  37.             {
  38.                 for (int j = 0; j < n; j++)
  39.                 {            
  40.                     if(word ==""){
  41.                         word = block[i][j].ToString();
  42.                         continue;
  43.                     }
  44.                
  45.                     next = block[i][j];
  46.                     char last = word[word.Length - 1];
  47.  
  48.                     if (((int)last) < ((int)next))
  49.                     {
  50.                         word += next.ToString();
  51.                     }
  52.                     else
  53.                     {
  54.                         all.Add(word);
  55.                         word = next.ToString(); ;
  56.                     }  
  57.                     if (j == n - 1)
  58.                     {
  59.                         all.Add(word);
  60.                         word = "";
  61.                         continue;
  62.                     }
  63.                 }
  64.             }
  65.  
  66.             for (int i = 0; i < n; i++)
  67.             {
  68.                 for (int j = 0; j < n; j++)
  69.                 {
  70.                     if (word == "")
  71.                     {
  72.                         word = block[j][i].ToString();
  73.                         continue;
  74.                     }
  75.  
  76.                     next = block[j][i];
  77.                     char last = word[word.Length - 1];
  78.  
  79.                     if (((int)last) < ((int)next))
  80.                     {
  81.                         word += next.ToString();
  82.                     }
  83.                     else
  84.                     {
  85.                         all.Add(word);
  86.                         word = next.ToString(); ;
  87.                     }
  88.                     if (j == n - 1)
  89.                     {
  90.                         all.Add(word);
  91.                         word = "";
  92.                         continue;
  93.                     }
  94.                 }
  95.             }
  96.        
  97.             for (int i = n-1; i >= 0; i--)
  98.             {
  99.                 for (int j = n - 1; j >= 0; j--)
  100.                 {
  101.                     if (word == "")
  102.                     {
  103.                         word = block[i][j].ToString();
  104.                         continue;
  105.                     }
  106.  
  107.                     next = block[i][j];
  108.                     char last = word[word.Length - 1];
  109.  
  110.                     if (((int)last) < ((int)next))
  111.                     {
  112.                         word += next.ToString();
  113.                     }
  114.                     else
  115.                     {
  116.                         all.Add(word);
  117.                         word = next.ToString(); ;
  118.                     }
  119.                     if (j == 0)
  120.                     {
  121.                         all.Add(word);
  122.                         word = "";
  123.                         continue;
  124.                     }
  125.                 }
  126.             }
  127.            
  128.         for (int i = n - 1; i >= 0; i--)
  129.         {
  130.             for (int j = n - 1; j >= 0; j--)
  131.             {
  132.                 if (word == "")
  133.                 {
  134.                     word = block[j][i].ToString();
  135.                     continue;
  136.                 }
  137.  
  138.                 next = block[j][i];
  139.                 char last = word[word.Length - 1];
  140.  
  141.                 if (((int)last) < ((int)next))
  142.                 {
  143.                     word += next.ToString();
  144.                 }
  145.                 else
  146.                 {
  147.                     all.Add(word);
  148.                     word = next.ToString(); ;
  149.                 }
  150.                 if (j == 0)
  151.                 {
  152.                     all.Add(word);
  153.                     word = "";
  154.                     continue;
  155.                 }
  156.             }
  157.         }
  158.  
  159.  
  160.         int longest = 0;
  161.         foreach(string value in all){
  162.             if (value.Length > longest)
  163.             {
  164.                 longest = value.Length;
  165.             }
  166.         }
  167.  
  168.         List<string> longest_arr = new List<string>();
  169.         foreach (string wo in all)
  170.         {
  171.             if (wo.Length == longest)
  172.             {
  173.                 string value = wo;
  174.  
  175.                 int index = longest_arr.IndexOf(value);
  176.                 if (index > -1)
  177.                 {
  178.  
  179.                 }
  180.                 else
  181.                 {
  182.                     longest_arr.Add(wo);
  183.                 }
  184.             }          
  185.         }
  186.  
  187.         var result = longest_arr[0];
  188.         foreach (string res in longest_arr)
  189.         {
  190.             if (String.Compare(res, result) < 0)
  191.             {
  192.                 result = res;
  193.             }
  194.         }
  195.         Console.Write(result);
  196.  
  197.         Console.ReadLine();
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment