1406번: 에디터
첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수
www.acmicpc.net

어떻게 풀어야할지 감이 안 잡혀서 갓구글의 도움을 받았다.
LinkedList로 하면 될 것 같아서 아래와 같이 짜보았다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import java.io.*;
import java.lang.invoke.SwitchPoint;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
LinkedList<Character> li = new LinkedList<>();
int index = 0;
String str = br.readLine();
index=str.length();
for(int i = 0; i < index; i++){
li.add(str.charAt(i));
}
int M = br.read()-48;
br.readLine();
while(M-- > 0){
String input = br.readLine();
char tmp = input.charAt(0);
switch (tmp){
case 'L':
if(index != 0)
index--;
break;
case 'D':
if(index != li.size())
index++;
break;
case 'B':
if(index != 0)
li.remove(index-1);
break;
case 'P':
char inChar = input.charAt(2);
li.add(index, inChar);
index++;
break;
}
}
Iterator iter = li.iterator();
while(iter.hasNext()) {
bw.write(li.poll());
}
bw.flush();
bw.close();
}
}
|
cs |
틀렸다.
또 구글링을 해봤다.
Stack을 이용해서 푸는 것을 보고 와.. 경이로웠다.
두 스택을 만들고.
커서기준 왼쪽과 오른쪽으로 나누어서
각각 맞는 스택에 넣는 풀이였다.
'L' 명령어일땐 왼쪽에 있는 스택이 비었는지 확인 후
왼쪽스택에 있던 값을 빼면서 오른쪽스택에 넣는다.
'D' 명령어일땐 오른쪽에 있는 스택이 비었는지 확인 후
오른쪽스택에 있던 값을 빼면서 왼쪽스택에 넣는다.
이런느낌으로 남은 두 명령어도 처리해주고,
마지막에는 이 두 스택들을 하나로 합쳐준다.
합치기 전 스택의 상황을 보면 아래 그림처럼 되어있을 것이다.

그렇기 때문에 12345 순서대로 출력되게끔 조작해주면 된다.
아래 그림처럼
왼쪽스택의 값들을 오른쪽스택으로 하나씩 옮겨주고,

오른쪽스택에 있는 값들을 출력해주면 끝~
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import java.io.*;
import java.lang.invoke.SwitchPoint;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Stack<Character> leftStk = new Stack<>();
Stack<Character> rightStk = new Stack<>();
String str = br.readLine();
for(int i = 0; i < str.length(); i++){
leftStk.push(str.charAt(i));
}
int M = Integer.parseInt(br.readLine());
while(M-- > 0){
String input = br.readLine();
char tmp = input.charAt(0);
switch (tmp){
case 'L':
if(!leftStk.empty())
rightStk.push(leftStk.pop());
break;
case 'D':
if(!rightStk.empty())
leftStk.push(rightStk.pop());
break;
case 'B':
if(!leftStk.empty())
leftStk.pop();
break;
case 'P':
char inputChar = input.charAt(2);
leftStk.push(inputChar);
break;
}
}
while(!leftStk.empty()){
rightStk.push(leftStk.pop());
}
while(!rightStk.empty()){
bw.write(rightStk.pop());
}
bw.flush();
bw.close();
}
}
|
cs |
'알고리즘 > 백준' 카테고리의 다른 글
| 백준 1158 요세푸스 자바[java] (0) | 2023.01.02 |
|---|---|
| 백준 10845 큐 자바[java] (0) | 2023.01.01 |
| 백준 1874번 스택 수열 자바[java] (0) | 2022.12.31 |
| 백준 9012번 괄호 자바[java] (0) | 2022.12.31 |
| 백준 9093번 단어 뒤집기 자바[java] (0) | 2022.12.31 |