본문 바로가기

알고리즘(백준 등) 공부/백준(자바)

백준 1360번: 되돌리기

https://www.acmicpc.net/problem/1360

type 명령 시, 문자를 오른쪽 끝에 추가하고 undo 명령 시, 몇초 전 작업을 역순으로 되돌리는 명령어들이 주어질 때 최종 문자열을 출력하는 문제이다.

추가 시, List에 몇초에 어떤 문자열이었는지 저장하고, undo 명령 시 해당 시간 전까지 돌아가서 문자열을 현재 시간과 함께 List에 저장한다. 이를 위한 Custom Class는 다음과 같다.

static class Node {
    
    int time;
    String content;
    
    Node(int time, String content) {
        this.time = time;
        this.content = content;
    }
}


List에는 몇초에 어떤 문자열인지 저장한다. 초기 상태는 0초, 빈 문자열이며, type은 직전 상태의 문자열에 문자를 추가한 상태를 시간과 함께 저장한다. undo는 현재시간 - 기간 보다 작으면서 가장 큰 시간에서의 상태가 현재 상태가 되게 저장한다.

List<Node> history = new ArrayList<>();
history.add(new Node(0, ""));

for (int i = 0; i < N; i++) {
    StringTokenizer st = new StringTokenizer(br.readLine());
    String command = st.nextToken();
    
    if (command.equals("type")) {
        String c = st.nextToken();
        int time = Integer.parseInt(st.nextToken());
        String lastContent = history.get(history.size() - 1).content;
        history.add(new Node(time, lastContent + c));
        
    } else {
        int duration = Integer.parseInt(st.nextToken());
        int currentTime = Integer.parseInt(st.nextToken());
        int targetTime = currentTime - duration;
        String undoContent = "";
        for (int j = history.size() - 1; j >= 0; j--) {
            if (history.get(j).time < targetTime) {
                undoContent = history.get(j).content;
                break;
            }
        }
        history.add(new Node(currentTime, undoContent));
    }
}



결과 코드는 다음과 같다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class 되돌리기1360 {
    
    static class Node {
        
        int time;
        String content;
        
        Node(int time, String content) {
            this.time = time;
            this.content = content;
        }
    }
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        List<Node> history = new ArrayList<>();
        history.add(new Node(0, ""));
        
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            String command = st.nextToken();
            
            if (command.equals("type")) {
                String c = st.nextToken();
                int time = Integer.parseInt(st.nextToken());
                String lastContent = history.get(history.size() - 1).content;
                history.add(new Node(time, lastContent + c));
                
            } else {
                int duration = Integer.parseInt(st.nextToken());
                int currentTime = Integer.parseInt(st.nextToken());
                int targetTime = currentTime - duration;
                String undoContent = "";
                for (int j = history.size() - 1; j >= 0; j--) {
                    if (history.get(j).time < targetTime) {
                        undoContent = history.get(j).content;
                        break;
                    }
                }
                history.add(new Node(currentTime, undoContent));
            }
        }
        System.out.print(history.get(history.size() - 1).content);
    }
}