본문 바로가기
코딩 테스트

5397번 키로거(자바, java) - 백준 문제풀이

by 주용사 2023. 1. 11.
728x90

커서에 집착하다가 엄청 틀렸다. 시간초과와 함께..

스택을 잘그려보면 커서라는 것이 필요가없다.

import java.io.*;
import java.util.*;

public class Main {
	// static int ncheck = 0;
	// static int nnum = 0;
	/* output setting */
	static StringBuilder sb = new StringBuilder();

	public static void main(String[] args) throws IOException {
		/* input reader */
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		// ArrayList<Integer> list = new ArrayList<>();
		StringTokenizer st;
		st = new StringTokenizer(br.readLine());

		int n = Integer.parseInt(st.nextToken());

		Stack<Character> lstack = new Stack<>();
		Stack<Character> rstack = new Stack<>();

		for (int ii = 0; ii < n; ii++) {
			st = new StringTokenizer(br.readLine());

			String temp = st.nextToken();
			char[] ctemp = temp.toCharArray();
			
			for (int zz = 0; zz < temp.length(); zz++) {
				if (ctemp[zz] == '<') {
					if (lstack.isEmpty() == false)
						rstack.push(lstack.pop());
					
				} else if (ctemp[zz] == '>') {
					if (rstack.isEmpty() == false)
						lstack.push(rstack.pop());
				} else if (ctemp[zz] == '-') {
					if (lstack.isEmpty() == false)
						lstack.pop();
				
				} else {
					lstack.push(ctemp[zz]);
				}
			}

			
			int lsize = lstack.size();

			for (int aa = 0; aa < lsize; aa++) {
				rstack.push(lstack.pop());

			}

			int rsize = rstack.size();

			for (int aa = 0; aa < rsize; aa++)
				sb.append(rstack.pop());
			sb.append('\n');
			

		}
		System.out.println(sb);

		br.close();
	}
}
728x90