๋ฌธ์
ํ์ด๊ณผ์
์ฒ์์๋ 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();
}
}
'Problem Solving' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋ฐฑ์ค 16946 - ๋ฒฝ ๋ถ์๊ณ ์ด๋ํ๊ธฐ 4 (0) | 2024.02.29 |
---|---|
[Java] ๋ฐฑ์ค 31091 - ๊ฑฐ์ง๋ง (0) | 2024.02.23 |
[Java] ๋ฐฑ์ค 31423 - ์ ์ด ํตํํฉ ๊ณํ (0) | 2024.02.21 |
[Java] ๋ฐฑ์ค 31248 - 3+1 ํ๋ ธ์ด ํ (0) | 2024.02.01 |
[C++] ๋ฐฑ์ค 17073 - ๋๋ฌด ์์ ๋น๋ฌผ (0) | 2023.02.04 |