View

๋ฌธ์ œ ๋งํฌ:

https://www.acmicpc.net/problem/1406

 

1406๋ฒˆ: ์—๋””ํ„ฐ

์ฒซ์งธ ์ค„์—๋Š” ์ดˆ๊ธฐ์— ํŽธ์ง‘๊ธฐ์— ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์€ ๊ธธ์ด๊ฐ€ N์ด๊ณ , ์˜์–ด ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 100,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ž…๋ ฅํ•  ๋ช…๋ น์–ด์˜ ๊ฐœ์ˆ˜

www.acmicpc.net

 

์†Œ์Šค ์ฝ”๋“œ:

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์„ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ƒ๊ฐํ–ˆ๋‹ค๋ฉด ๊ฐ„๋‹จํžˆ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๐Ÿ˜‚

Share Link
reply
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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