알고리즘 풀이/백준

[백준][Java] 9935번 문자열 폭발

배게 2019. 3. 25. 05:37
728x90

빠른 연산을 위해 Scanner가 아닌 BufferedReader 사용


 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
import java.io.*;
import java.util.*;





public class Main {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//		Scanner sc = new Scanner(System.in);
		String input = br.readLine();
		String bomb = br.readLine();
		
		Stack<Character> stack = new Stack<>();
		
		for(int i=input.length()-1; i>=0; i--) {
			boolean isBomb = false;
			char c = input.charAt(i);
			
			stack.push(c);
			
			if(c == bomb.charAt(0) && stack.size()>= bomb.length()) {
				isBomb = true;
				for(int j=0 ;j<bomb.length() ;j++) {
					if( bomb.charAt(j) != stack.get(stack.size()-1-j)) {
						isBomb = false;
						break;
					}
				}
				if (isBomb) {
					for(int k=0; k<bomb.length(); k++) {
						stack.pop();
					}
				}
			}
		}
		
		StringBuffer sb = new StringBuffer();
		if(stack.isEmpty()) System.out.println("FRULA");
		else {
			for(int i=stack.size()-1 ; i>=0 ; i--) {
//				System.out.print(stack.get(i));
				sb.append(stack.pop());
			}
		}
		System.out.println(sb);
		
	 
	}
}