본문 바로가기
온라인 저지/BOJ

[BOJ/Java] 1406 에디터

by ahj 2022. 2. 13.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;

public class Main {

	static BufferedReader br;
	static StringBuilder sb;
	static LinkedList<Character> q;
	static char tmpCharList[];
	static int cursor, m;
	static String str;

	public static void main(String[] args) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();

		tmpCharList = br.readLine().toCharArray();
		q = new LinkedList<>();
		for (char el : tmpCharList) {
			q.add(el);
		}
		cursor = q.size();
		m = Integer.parseInt(br.readLine());
		for (int i = 0; i < m; i++) {
			str = br.readLine();
			switch (str.charAt(0)) {
			case 'L':
				if (cursor == 0)
					continue;
				q.addFirst(q.pollLast());
				cursor--;
				break;
			case 'D':
				if (cursor == q.size())
					continue;
				q.add(q.poll());
				cursor++;
				break;
			case 'B':
				if (cursor == 0)
					continue;
				q.pollLast();
				cursor--;
				break;
			case 'P':
				q.add(str.charAt(2));
				cursor++;
				break;
			}
		}
		for (int i = 0; i < q.size() - cursor; i++) {
			q.add(q.poll());
		}
		while (!q.isEmpty()) {
			sb.append(q.poll());
		}
		System.out.println(sb);
	}
}

'온라인 저지 > BOJ' 카테고리의 다른 글

[BOJ/Java] 11047 동전 0  (0) 2022.02.13
[BOJ/Java] 1436 영화감독 숌  (0) 2022.02.13
[BOJ/Java] 1021 회전하는 큐  (0) 2022.02.13
[BOJ/Java] 2563 색종이  (0) 2022.02.11
[BOJ/Java] 1158 요세푸스 문제  (0) 2022.02.10

댓글