Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Deque;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int lines = Integer.parseInt(scan.nextLine());
- StringBuilder text = new StringBuilder();
- Deque<StringBuilder> stack = new ArrayDeque();
- while (lines-- > 0){
- String[] line = scan.nextLine().split(" ");
- String comand = line[0];
- switch (comand){
- case "1":
- text.append(line[1]);
- stack.push(new StringBuilder(text));
- break;
- case "2":
- int count = Integer.parseInt(line[1]);
- int start = text.length() - count;
- text.delete(start, start + count);
- stack.push(new StringBuilder(text));
- break;
- case "3":
- System.out.println(text.charAt(Integer.parseInt(line[1])-1));
- break;
- case "4":
- if (stack.size() > 1){
- stack.pop();
- text = stack.peek();
- }else if (stack.size() == 1){
- text = new StringBuilder();
- }
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment