Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- Stack<String> final1 = new Stack<>();
- ArrayDeque<String> deleted = new ArrayDeque<>();
- ArrayDeque<String> added = new ArrayDeque<>();
- ArrayDeque<Integer> commands = new ArrayDeque<>();
- while (n-- > 0) {
- String[] command = scanner.nextLine().split(" ");
- switch (command[0]) {
- case "1":
- commands.push(Integer.parseInt(command[0]));
- added.push(command[1]);
- final1.push(command[1]);
- break;
- case "2":
- commands.push(Integer.parseInt(command[0]));
- String deletedWords = "";
- int toDelete = Integer.parseInt(command[1]);
- while (!deleted.isEmpty() && toDelete-- > 0) {
- deletedWords += final1.pop();
- }
- break;
- case "3":
- break;
- case "4":
- if(!commands.isEmpty() && commands.peek() == 1) {
- commands.pop();
- final1.pop();
- }
- else if(!commands.isEmpty() && commands.peek() == 2 && !deleted.isEmpty()){
- commands.pop();
- final1.push(deleted.pop());
- }
- break;
- }
- }
- System.out.println(final1.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement