카테고리 없음

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

C0MPAS 2024. 5. 7. 16:11

5월 7일(화) - 자료 구조(9935번)

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


최초 생각 정리


문제점


풀이

(풀이출처 -> https://jgeun97.tistory.com/200)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String str = br.readLine();
        String bomb = br.readLine();

        Stack<Character> stack = new Stack<>();
        for(int i=0; i<str.length(); i++)
        {
            stack.push(str.charAt(i));

            if(stack.size() >= bomb.length())
            {
                boolean compare_with_bomb = true;
                for(int j=0; j<bomb.length(); j++)
                {
                    if(stack.get(stack.size() - bomb.length() + j) != bomb.charAt(j))
                    {
                        compare_with_bomb = false;
                        break;
                    }
                }

                if(compare_with_bomb)
                {
                    for(int j=0; j<bomb.length(); j++)
                    {
                        stack.pop();
                    }
                }
            }
        }

        for(char ch : stack)
        {
            sb.append(ch);
        }

        System.out.println((sb.length()>0) ? sb.toString() : "FRULA");
    }
}