Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Deque;
  3. import java.util.Scanner;
  4.  
  5. public class SimpleTextEditor {
  6. public static void main(String[] args) {
  7. Scanner scanner=new Scanner(System.in);
  8.  
  9. int n=Integer.parseInt(scanner.nextLine());
  10.  
  11. StringBuilder text=new StringBuilder();
  12. Deque<StringBuilder> stack=new ArrayDeque<>();
  13.  
  14. while (n-->0){
  15. String[] tokens=scanner.nextLine().split("\\s+");
  16.  
  17. String commmand=tokens[0];
  18. switch (commmand){
  19. case "1":
  20.  
  21. text.append(tokens[1]);
  22. stack.push(new StringBuilder(text));
  23. break;
  24. case "2":
  25. int count=Integer.parseInt(tokens[1]);
  26. int start=text.length()-count;
  27. text.delete(start,start+count);
  28. stack.push(new StringBuilder(text));
  29. break;
  30. case "3":
  31.  
  32. System.out.println(text.charAt(Integer.parseInt(tokens[1])));
  33. break;
  34. case "4":
  35. if(stack.size()>1) {
  36. stack.pop();
  37. text = stack.peek();
  38. }
  39. break;
  40.  
  41. }
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement