Advertisement
BojidarDosev

Untitled

May 19th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int n = Integer.parseInt(scanner.nextLine());
  8.  
  9. Stack<String> final1 = new Stack<>();
  10.  
  11. ArrayDeque<String> deleted = new ArrayDeque<>();
  12. ArrayDeque<String> added = new ArrayDeque<>();
  13. ArrayDeque<Integer> commands = new ArrayDeque<>();
  14.  
  15. while (n-- > 0) {
  16. String[] command = scanner.nextLine().split(" ");
  17. switch (command[0]) {
  18. case "1":
  19. commands.push(Integer.parseInt(command[0]));
  20. added.push(command[1]);
  21. final1.push(command[1]);
  22. break;
  23. case "2":
  24. commands.push(Integer.parseInt(command[0]));
  25. String deletedWords = "";
  26. int toDelete = Integer.parseInt(command[1]);
  27. while (!deleted.isEmpty() && toDelete-- > 0) {
  28. deletedWords += final1.pop();
  29. }
  30. break;
  31. case "3":
  32.  
  33. break;
  34. case "4":
  35. if(!commands.isEmpty() && commands.peek() == 1) {
  36. commands.pop();
  37. final1.pop();
  38. }
  39. else if(!commands.isEmpty() && commands.peek() == 2 && !deleted.isEmpty()){
  40. commands.pop();
  41. final1.push(deleted.pop());
  42. }
  43. break;
  44. }
  45. }
  46. System.out.println(final1.toString());
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement