본문으로 바로가기

백준 9935번 문자열 폭발 (JAVA)

category 카테고리 없음 2021. 7. 12. 19:29

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

 

9935번: 문자열 폭발

첫째 줄에 문자열이 주어진다. 문자열의 길이는 1보다 크거나 같고, 1,000,000보다 작거나 같다. 둘째 줄에 폭발 문자열이 주어진다. 길이는 1보다 크거나 같고, 36보다 작거나 같다. 두 문자열은 모

www.acmicpc.net

 

2021 네이버웹툰 코딩테스트 3번이랑 굉장히 유사한 문제이다.

떨어진게 억울해서 복습할 겸 풀어보았다.

슬라이딩윈도우로 구현하였다.

 

public class BOJ_9935_문자열폭발 {
	static StringBuilder sb = new StringBuilder();
	static String bomb, str;
	static int bomblen;

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		str = br.readLine();
		bomb = br.readLine();
		bomblen = bomb.length();
		for (int i = 0; i < str.length(); i++) {
			sb.append(str.charAt(i));
			if (sb.length() >= bomblen) {
				if (sb.substring(sb.length() - bomblen, sb.length()).equals(bomb)) {
					sb.delete(sb.length() - bomblen, sb.length());
				}
			}
		}
		System.out.println(sb.length() == 0 ? "FRULA" : sb);
	}
}