fork(2) download
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner sc = new Scanner(System.in);
  8.  
  9.  
  10. int N = sc.nextInt();
  11.  
  12.  
  13. //type
  14. String[] type = new String[N];
  15.  
  16. //arr
  17. String[] arr = new String[N];
  18.  
  19. //time
  20. Integer[] time = new Integer[N];
  21.  
  22. //print yes or no
  23. boolean[] check = new boolean[N];
  24.  
  25. sc.nextLine();
  26. for (int i = 0; i < N; i++) {
  27. String line = sc.nextLine(); // 한 줄 전체 읽기
  28. String[] parts = line.split(" "); // 공백 기준으로 나누기
  29.  
  30. type[i] = parts[0]; // 첫 번째 단어가 type
  31.  
  32. arr[i] = parts[1]; // 두 번째 값
  33. time[i] = Integer.parseInt(parts[2]); // 세 번째 값 (숫자)
  34. }
  35.  
  36. List<String> tpyelist = Arrays.asList(type);
  37. Collections.reverse(tpyelist);
  38. type = tpyelist.toArray(new String[0]);
  39.  
  40. List<String> arrlist = Arrays.asList(arr);
  41. Collections.reverse(arrlist);
  42. arr = arrlist.toArray(new String[0]);
  43.  
  44.  
  45. List<Integer> timelist = Arrays.asList(time);
  46. Collections.reverse(timelist);
  47. time = timelist.toArray(new Integer[0]);
  48.  
  49. for (int i = 0; i < N; i++) {
  50. String typeValue = type[i];
  51.  
  52. if (typeValue.equals("undo")) {
  53. if (!check[i]) {
  54. int removeTime = Math.abs(time[i] - Integer.parseInt(arr[i]));
  55. for (int j = i; j < (i + removeTime)-1; j++) {
  56. if(j >= N) break;
  57. else {
  58. check[j] = !check[j];
  59. }
  60. }
  61. }
  62. }
  63. }
  64. for (int i = 0; i < N / 2; i++) {
  65. boolean temp = check[i];
  66. check[i] = check[N - 1 - i];
  67. check[N - 1 - i] = temp;
  68. }
  69.  
  70. for (int i=N-1; i>=0; i--) {
  71. if(check[i] && !type[i].equals("undo")) System.out.print(arr[i]);
  72. }
  73. }
  74. }
Success #stdin #stdout 0.15s 56564KB
stdin
5
type z 1
undo 1 2
undo 1 3
undo 1 4
undo 100000 5
stdout
z