Advertisement
Guest User

Untitled

a guest
Jun 5th, 2022
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 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 || endIndex < 0)
  29.                     {
  30.                         if (endIndex < 0)
  31.                         {
  32.                             currentOutput = Console.ReadLine();
  33.                             continue;
  34.                         }
  35.  
  36.                         startIndex = 0;
  37.                     }
  38.  
  39.                     MerageStrings(strings, startIndex, endIndex);
  40.                 }
  41.                 else if (currentCommand[0] == "divide")
  42.                 {
  43.                     DivideStrings(strings, currentCommand);
  44.                 }
  45.              
  46.                 currentOutput = Console.ReadLine();
  47.             }
  48.  
  49.             Console.WriteLine(string.Join(' ', strings));
  50.         }
  51.  
  52.         private static void DivideStrings(List<string> strings, List<string> currentCommand)
  53.         {
  54.             int index = int.Parse(currentCommand[1]);
  55.             int partition = int.Parse(currentCommand[2]);
  56.  
  57.             string currentString = strings[index];
  58.             int lengthOfSubstrings = currentString.Length;
  59.  
  60.             strings[index] = string.Empty;
  61.  
  62.             if (lengthOfSubstrings % partition == 0)
  63.             {
  64.                 lengthOfSubstrings /= partition;
  65.  
  66.                 for (int currentSymbol = 0; currentSymbol < currentString.Length; currentSymbol++)
  67.                 {
  68.                     if (currentSymbol != 0 && currentSymbol % lengthOfSubstrings == 0)
  69.                     {
  70.                         index++;
  71.                         strings.Insert(index, string.Empty);
  72.                     }
  73.  
  74.                     strings[index] += currentString[currentSymbol];
  75.                 }
  76.             }
  77.             else
  78.             {
  79.                 lengthOfSubstrings = lengthOfSubstrings / partition <= 0 ? 1 : lengthOfSubstrings/ partition;
  80.  
  81.                 int countOfSubstrings = partition;
  82.  
  83.                 for (int currentSymbol = 0; currentSymbol < currentString.Length; currentSymbol++)
  84.                 {
  85.                     if (currentSymbol != 0 && currentSymbol % lengthOfSubstrings == 0 && countOfSubstrings > 1)
  86.                     {
  87.                         index++;
  88.                         strings.Insert(index, string.Empty);
  89.                         countOfSubstrings--;
  90.                     }
  91.  
  92.                     strings[index] += currentString[currentSymbol];
  93.                 }
  94.             }
  95.         }
  96.  
  97.         private static void MerageStrings(List<string> strings, int startIndex, int endIndex)
  98.         {
  99.             int counter = startIndex;
  100.  
  101.             for (int currentIndex = startIndex; currentIndex < strings.Count - 1; currentIndex++)
  102.             {
  103.                 strings[startIndex] += strings[startIndex + 1];
  104.                 strings.RemoveAt(startIndex + 1);
  105.                 currentIndex = startIndex - 1;
  106.                 counter++;
  107.  
  108.                 if (counter == endIndex)
  109.                 {
  110.                     break;
  111.                 }
  112.             }
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement