Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.source.tree.IfTree;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class AnonymousThreat {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String lineToMOrD = scanner.nextLine();
- List<String> lineToList = Arrays.stream(lineToMOrD.split("\\s+")).collect(Collectors.toList());
- String command = "";
- while (!"3:1".equals(command = scanner.nextLine())) {
- String[] commArr = command.split("\\s+");
- switch (commArr[0]) {
- case "merge":
- int startIndex = Integer.parseInt(commArr[1]);
- int endIndex = Integer.parseInt(commArr[2]);
- String toMerge = "";
- if (startIndex < 0) {
- startIndex = 0;
- } else if (startIndex > lineToList.size() - 1) {
- startIndex = lineToList.size() - 1;
- }
- if (endIndex > lineToList.size() - 1) {
- endIndex = lineToList.size() - 1;
- } else if (endIndex < 0) {
- endIndex = 0;
- }
- for (int i = startIndex; i < endIndex; i++) {
- toMerge = lineToList.get(i) + lineToList.get(i + 1);
- lineToList.set(startIndex, toMerge);
- lineToList.remove(i + 1);
- endIndex--;
- i -= 1;
- }
- break;
- case "divide":
- List<String> add = new ArrayList<>();
- int indexDiv = Integer.parseInt(commArr[1]);
- int parts = Integer.parseInt(commArr[2]);
- String checkIfDiv = lineToList.get(indexDiv);
- int subsLength = checkIfDiv.length() / parts;
- if (checkIfDiv.length() % parts != 0) {
- parts--;
- for (int i = 0; i < parts; i++) {
- add.add(checkIfDiv.substring(0, subsLength));
- checkIfDiv = checkIfDiv.replace(checkIfDiv.substring(0, subsLength), "");
- }
- add.add(checkIfDiv);
- lineToList.remove(indexDiv);
- lineToList.addAll(indexDiv, add);
- } else {
- for (int i = 0; i < parts; i++) {
- add.add(checkIfDiv.substring(0, subsLength));
- checkIfDiv = checkIfDiv.replace(checkIfDiv.substring(0, subsLength), "");
- }
- lineToList.remove(indexDiv);
- lineToList.addAll(indexDiv, add);
- }
- break;
- }
- }
- System.out.println(lineToList.toString().replaceAll("[\\[\\],]",""));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement