Advertisement
ChonoChonovUk

Anonymous Threat New Divide

Oct 21st, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import com.sun.source.tree.IfTree;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class AnonymousThreat {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         String lineToMOrD = scanner.nextLine();
  13.         List<String> lineToList = Arrays.stream(lineToMOrD.split("\\s+")).collect(Collectors.toList());
  14.         String command = "";
  15.  
  16.         while (!"3:1".equals(command = scanner.nextLine())) {
  17.             String[] commArr = command.split("\\s+");
  18.             switch (commArr[0]) {
  19.                 case "merge":
  20.  
  21.                     int startIndex = Integer.parseInt(commArr[1]);
  22.                     int endIndex = Integer.parseInt(commArr[2]);
  23.  
  24.  
  25.                     String toMerge = "";
  26.  
  27.  
  28.                     if (startIndex < 0) {
  29.                         startIndex = 0;
  30.                     } else if (startIndex > lineToList.size() - 1) {
  31.                         startIndex = lineToList.size() - 1;
  32.                     }
  33.  
  34.                     if (endIndex > lineToList.size() - 1) {
  35.  
  36.                         endIndex = lineToList.size() - 1;
  37.  
  38.                     } else if (endIndex < 0) {
  39.                         endIndex = 0;
  40.                     }
  41.  
  42.                     for (int i = startIndex; i < endIndex; i++) {
  43.  
  44.                         toMerge = lineToList.get(i) + lineToList.get(i + 1);
  45.  
  46.                         lineToList.set(startIndex, toMerge);
  47.  
  48.                         lineToList.remove(i + 1);
  49.  
  50.                         endIndex--;
  51.                         i -= 1;
  52.                     }
  53.  
  54.  
  55.                     break;
  56.                 case "divide":
  57.  
  58.                     List<String> add = new ArrayList<>();
  59.                     int indexDiv = Integer.parseInt(commArr[1]);
  60.                     int parts = Integer.parseInt(commArr[2]);
  61.                     String checkIfDiv = lineToList.get(indexDiv);
  62.                     int subsLength = checkIfDiv.length() / parts;
  63.  
  64.  
  65.                     if (checkIfDiv.length() % parts != 0) {
  66.                         parts--;
  67.                         for (int i = 0; i < parts; i++) {
  68.                             add.add(checkIfDiv.substring(0, subsLength));
  69.                             checkIfDiv = checkIfDiv.replace(checkIfDiv.substring(0, subsLength), "");
  70.                         }
  71.  
  72.                         add.add(checkIfDiv);
  73.                         lineToList.remove(indexDiv);
  74.                         lineToList.addAll(indexDiv, add);
  75.  
  76.                     } else {
  77.                         for (int i = 0; i < parts; i++) {
  78.                             add.add(checkIfDiv.substring(0, subsLength));
  79.                             checkIfDiv = checkIfDiv.replace(checkIfDiv.substring(0, subsLength), "");
  80.                         }
  81.  
  82.                         lineToList.remove(indexDiv);
  83.                         lineToList.addAll(indexDiv, add);
  84.                     }
  85.  
  86.                     break;
  87.             }
  88.  
  89.             }
  90.         System.out.println(lineToList.toString().replaceAll("[\\[\\],]",""));
  91.         }
  92.  
  93.  
  94.  
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement