Advertisement
Guest User

Untitled

a guest
Jun 5th, 2022
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace P08.AnonymousThreat
  6. {
  7.     internal class AnonymousThreat
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> strings = Console.ReadLine()
  12.                 .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  13.                 .ToList();
  14.  
  15.             string currentOutput = Console.ReadLine();
  16.  
  17.             while (currentOutput != "3:1")
  18.             {
  19.                 List<string> currentCommand = currentOutput
  20.                     .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  21.                     .ToList();
  22.  
  23.                 if (currentCommand[0] == "merge")
  24.                 {
  25.                     int startIndex = int.Parse(currentCommand[1]);
  26.                     int endIndex = int.Parse(currentCommand[2]);
  27.  
  28.                     if (startIndex < 0)
  29.                     {
  30.                         startIndex = 0;
  31.                     }
  32.                     if (endIndex > strings.Count - 1)
  33.                     {
  34.                         endIndex = strings.Count - 1;
  35.                     }
  36.  
  37.                     MerageStrings(strings, startIndex, endIndex);
  38.                 }
  39.                 else if (currentCommand[0] == "divide")
  40.                 {
  41.                     DivideStrings(strings, currentCommand);
  42.                 }
  43.  
  44.                 currentOutput = Console.ReadLine();
  45.             }
  46.  
  47.             Console.WriteLine(string.Join(' ', strings));
  48.         }
  49.  
  50.         private static void DivideStrings(List<string> strings, List<string> currentCommand)
  51.         {
  52.             int index = int.Parse(currentCommand[1]);
  53.             int partition = int.Parse(currentCommand[2]);
  54.  
  55.             string currentString = strings[index];
  56.             int lengthOfSubstrings = currentString.Length;
  57.  
  58.             strings[index] = string.Empty;
  59.  
  60.             if (lengthOfSubstrings % partition == 0)
  61.             {
  62.                 lengthOfSubstrings /= partition;
  63.  
  64.                 for (int currentSymbol = 0; currentSymbol < currentString.Length; currentSymbol++)
  65.                 {
  66.                     if (currentSymbol != 0 && currentSymbol % lengthOfSubstrings == 0)
  67.                     {
  68.                         index++;
  69.                         strings.Insert(index, string.Empty);
  70.                     }
  71.  
  72.                     strings[index] += currentString[currentSymbol];
  73.                 }
  74.             }
  75.             else
  76.             {
  77.                 lengthOfSubstrings = lengthOfSubstrings / partition <= 0 ? 1 : lengthOfSubstrings / partition;
  78.  
  79.                 int countOfSubstrings = partition;
  80.  
  81.                 for (int currentSymbol = 0; currentSymbol < currentString.Length; currentSymbol++)
  82.                 {
  83.                     if (currentSymbol != 0 && currentSymbol % lengthOfSubstrings == 0 && countOfSubstrings > 1)
  84.                     {
  85.                         index++;
  86.                         strings.Insert(index, string.Empty);
  87.                         countOfSubstrings--;
  88.                     }
  89.  
  90.                     strings[index] += currentString[currentSymbol];
  91.                 }
  92.             }
  93.         }
  94.  
  95.         private static void MerageStrings(List<string> strings, int startIndex, int endIndex)
  96.         {
  97.             for (int j = startIndex + 1; j <= endIndex; j++)
  98.             {
  99.                 strings[startIndex] += strings[startIndex + 1];
  100.                 strings.RemoveAt(startIndex + 1);
  101.             }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement