Advertisement
georgi_yankov

MagicWords

Dec 26th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. class MagicWords
  7. {
  8.     static void Main()
  9.     {
  10.         int numberOfWords = int.Parse(Console.ReadLine());
  11.         string[] words = new string[numberOfWords];
  12.  
  13.         for (int i = 0; i < numberOfWords; i++)
  14.         {
  15.             words[i] = Console.ReadLine();
  16.         }
  17.  
  18.         words = Reorder(words, numberOfWords);
  19.         Console.WriteLine(Print(words));
  20.     }
  21.  
  22.     private static string[] Reorder(string[] words, int numberOfWords)
  23.     {
  24.         List<string> list = words.OfType<string>().ToList();
  25.  
  26.         for (int i = 0; i < list.Count; i++)
  27.         {
  28.             string currentWord = list[i];
  29.             int newIndex = (currentWord.Length % (numberOfWords + 1)) - 1;
  30.  
  31.             if (newIndex == -1)
  32.             {
  33.                 newIndex = 0;
  34.             }
  35.  
  36.             string buffer = list[i];
  37.             list.Remove(buffer);
  38.             list.Insert(newIndex, buffer);
  39.         }
  40.  
  41.         words = list.ToArray();
  42.  
  43.         return words;
  44.     }
  45.  
  46.     private static string Print(string[] words)
  47.     {
  48.         StringBuilder result = new StringBuilder();
  49.         int index = 0;
  50.  
  51.         var sorted = words.OrderBy(n => n.Length);
  52.         var longest = sorted.LastOrDefault();
  53.  
  54.         while (index < longest.Length)
  55.         {
  56.             for (int i = 0; i < words.Length; i++)
  57.             {
  58.                 string currentWord = words[i];
  59.  
  60.                 if (index < currentWord.Length)
  61.                 {
  62.                     result.Append(currentWord[index]);
  63.                 }
  64.             }
  65.  
  66.             index++;
  67.         }
  68.  
  69.         return result.ToString();
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement