View
๋ฌธ์ ๋งํฌ:
https://www.acmicpc.net/problem/1406
์์ค ์ฝ๋:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
private Stack<Character> stackL;
private Stack<Character> stackR;
public Main(Stack<Character> stackL, Stack<Character> stackR) {
this.stackL = stackL;
this.stackR = stackR;
}
public static void main(String[] args) {
Main main = new Main(new Stack<Character>(), new Stack<Character>());
main.solve();
}
public void solve() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String inputStr = br.readLine();
int m = Integer.parseInt(br.readLine());
// 1. ์ฒ์ ์
๋ ฅ๋ฐ์ ๋ฌธ์์ด์ stackL์ ์ ์ฅ
for (int i = 0; i < inputStr.length(); i++) {
stackL.push(inputStr.charAt(i));
}
// 2. m๋ฒ๋งํผ ์๋ํฐ ์ํ
while (m-- > 0)
editor(br.readLine());
// 3. stackL๊ณผ stackR์ ์๋ char ๋ฌธ์ ํฉ์น๊ธฐ
while (!stackL.isEmpty())
stackR.push(stackL.pop());
while (!stackR.isEmpty())
bw.write(stackR.pop());
bw.flush();
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void editor(String command) {
switch (String.valueOf(command.charAt(0))) {
case "L":
if (stackL.isEmpty()) break;
stackR.push(stackL.pop());
break;
case "D":
if (stackR.isEmpty()) break;
stackL.push(stackR.pop());
break;
case "B":
if (stackL.isEmpty()) break;
stackL.pop();
break;
case "P":
StringTokenizer st = new StringTokenizer(command);
st.nextToken();
char addChar = st.nextToken().charAt(0);
stackL.push(addChar);
break;
default:
break;
}
}
}
์ฒ์๋ถํฐ ์ปค์ ์์น ๋์ stack์ ์ผ์ชฝ, ์ค๋ฅธ์ชฝ์ผ๋ก ๋๋์ด์ ์๊ฐํ๋ค๋ฉด ๊ฐ๋จํ ํ ์ ์๋ ๋ฌธ์ ์๋ค. ๐
'๊ฐ๋ฐ > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] 42586๋ฒ: ๊ธฐ๋ฅ ๊ฐ๋ฐ (JAVA) (0) | 2022.06.20 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] 17677๋ฒ: ๋ด์ค ํด๋ฌ์คํฐ๋ง (JAVA) (0) | 2022.06.20 |
[ํ๋ก๊ทธ๋๋จธ์ค] 92334๋ฒ: ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ (JAVA) (0) | 2022.06.06 |
[๋ฐฑ์ค/BOJ] 15953๋ฒ: ์๊ธ ํํฐ (JAVA) (0) | 2022.06.05 |
reply