Advertisement
anilak

Magic Words

Sep 14th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class MagicWords
  5. {
  6.     static void Print(string[] words, int[] positions, int maxLen)
  7.     {
  8.         StringBuilder output = new StringBuilder();
  9.         for (int i = 0; i < maxLen; i++)
  10.         {
  11.             for (int j = 0; j < words.Length; j++)
  12.             {
  13.                 if (i >= words[positions[j]].Length) continue;
  14.                 output.Append(words[positions[j]][i]);
  15.             }
  16.         }
  17.         Console.WriteLine(output);
  18.     }
  19.  
  20.     static void Swap(int[] arr, int pos1, int pos2)
  21.     {
  22.         int temp = arr[pos1];
  23.         arr[pos1] = arr[pos2];
  24.         arr[pos2] = temp;
  25.     }
  26.  
  27.     static void Main()
  28.     {
  29.         int n = int.Parse(Console.ReadLine());
  30.         string[] words = new string[n];
  31.         int maxLen = 0;
  32.         int[] positions = new int[n];
  33.         for (int i = 0; i < n; i++)
  34.         {
  35.             words[i] = Console.ReadLine();
  36.             positions[i] = i;
  37.             if (maxLen < words[i].Length)
  38.             {
  39.                 maxLen = words[i].Length;
  40.             }
  41.         }
  42.         for (int i = 0; i < n; i++)
  43.         {
  44.             int newPos = words[positions[i]].Length % (n + 1);
  45.             if (newPos > i + 1)
  46.             {
  47.                 for (int j = 0; j < Math.Abs(newPos - i - 1); j++)
  48.                 {
  49.                     Swap(positions, i + j, i + 1 + j);
  50.                 }
  51.             }
  52.             else if(newPos < i)
  53.             {
  54.                 for (int j = 0; j < Math.Abs(newPos - i); j++)
  55.                 {
  56.                     Swap(positions, i - j, i - 1 - j);
  57.                 }
  58.             }
  59.         }
  60.         Print(words, positions, maxLen);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement