๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Problem Solving

[Java] ๋ฐฑ์ค€ 1406 - ์—๋””ํ„ฐ

๋ฌธ์ œ


ํ’€์ด๊ณผ์ •

์ฒ˜์Œ์—๋Š” int ๋ณ€์ˆ˜์— ์ปค์„œ์˜ ์œ„์น˜๋ฅผ ๊ธฐ๋กํ•˜๊ณ  LinkedList์˜ add, remove ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ’€์—ˆ์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ. ๊ฒฐ๊ตญ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ํ’€์—ˆ์Œ.

๋”๋ณด๊ธฐ
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		char[] initialText = br.readLine().toCharArray();
		int M = Integer.parseInt(br.readLine());
		char[] text = new char[600001];
		int[] pre = new int[600001];
		int[] nxt = new int[600001];
		int cursor = initialText.length;
		int unused = cursor + 1;

		for (int i = 0; i < cursor; i++) {
			text[i] = initialText[i];
			pre[i] = i - 1;
			nxt[i] = i + 1;
		}
		pre[0] = -1;
		pre[cursor] = cursor - 1;
		nxt[cursor] = -1;

		while (M-- > 0) {
			switch (br.read()) {
			case 'L':
				if (pre[cursor] > -1) {
					cursor = pre[cursor];
				}
				break;
			case 'D':
				if (nxt[cursor] > -1) {
					cursor = nxt[cursor];
				}
				break;
			case 'B':
				if (pre[cursor] > -1) {
					if (pre[pre[cursor]] > -1) {
						nxt[pre[pre[cursor]]] = cursor;
					}
					pre[cursor] = pre[pre[cursor]];
				}
				break;
			case 'P':
				br.read();
				text[unused] = (char) br.read();
				pre[unused] = pre[cursor];
				nxt[unused] = cursor;
				if (pre[cursor] > -1) {
					nxt[pre[cursor]] = unused;
				}
				pre[cursor] = unused;
				unused++;
				break;
			default:
				break;
			}
			br.readLine();
		}

		while (pre[cursor] > -1) {
			cursor = pre[cursor];
		}
		
		while (nxt[cursor] > -1) {
			sb.append(text[cursor]);
			cursor = nxt[cursor];
		}

		System.out.println(sb.toString());
		br.close();

	}

}

 

๊ตฌ๊ธ€๋งํ•ด๋ณด๋‹ˆ LinkedList์˜ add์™€ remove ๋ฉ”์†Œ๋“œ๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค๊นŒ์ง€ ํƒ์ƒ‰ํ•˜์—ฌ ์ ‘๊ทผํ•˜๊ธฐ ๋•Œ๋ฌธ์— O(n)์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆผ. ์ด๋Ÿด๋•Œ๋Š” ListIterator ํ™œ์šฉํ•˜๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ ์ญ‰ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ iterator์˜ ์œ„์น˜์—์„œ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ ๊ฐ€๋Šฅ. 

๋”๋ณด๊ธฐ
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.ListIterator;

public class Main {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		LinkedList<Character> list = new LinkedList<>();
		char[] initialText = br.readLine().toCharArray();
		int N = initialText.length;
		int M = Integer.parseInt(br.readLine());

		for (int i = 0; i < N; i++) {
			list.add(initialText[i]);
		}

		ListIterator<Character> cursor = list.listIterator(N);

		while (M-- > 0) {
			switch (br.read()) {
			case 'L':
				if (cursor.hasPrevious()) {
					cursor.previous();
				}
				break;
			case 'D':
				if (cursor.hasNext()) {
					cursor.next();
				}
				break;
			case 'B':
				if (cursor.hasPrevious()) {
					cursor.previous();
					cursor.remove();
					N--;
				}
				break;
			case 'P':
				br.read();
				cursor.add((char) br.read());
				N++;
				break;
			default:
				break;
			}
			br.readLine();
		}

		for (char i : list) {
			sb.append(i);
		}
		System.out.println(sb.toString());
		br.close();

	}

}