코테 문제풀이 7일차

아빠는 외계연·2023년 1월 2일
0

CodingTest

목록 보기
9/18

👑문제: [BOJ Gold3]

HTML 파싱

풀이 시 고려해야 할 점

문제는 매우 간단하지만 설명이 미숙했다.
처음에는 특정 꺽쇠만 존재하는 줄 알았다만,, 모든 꺽쇠가 가능했던것! -> 이것은 정규식으로 해결가능하였다.

풀이 과정

package BOJ.IP;

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

public class G22859 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static void check(String str) {

        // 공백제거
        str = str.trim();

        // 태그 지우기
        str = str.replaceAll("<[^<>]+>","");
        str = str.replaceAll(" {2,}"," ");
        System.out.println(str);
    }

    public static void main(String[] args) throws IOException {
        String str = br.readLine();

        //main 걸러내기
        String[] arr = str.split("<main>|</main>");
        Queue<String> queue = new LinkedList<>();

        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != "") {
                String[] tmp = arr[i].split("<div title=\"|\">|</div>");
                for (int j = 0; j < tmp.length; j++) {
                    if(tmp[j] != "") {
                        queue.add(tmp[j]);
                    }
                }
            }
        }

        while(!queue.isEmpty()) {
            String[] tmp = queue.poll().split("<p>|</p>");
            if(tmp.length == 1) {
                System.out.println("title : " + tmp[0]);
            }else {
                for (int i = 0; i < tmp.length; i++) {
                    if(tmp[i]!="") {
                        check(tmp[i]);
                    }
                }
            }
        }
    }
}

느낀점

구냥 정규식 잘쓰면 됬던 문제~ 별로 좋은문제는 아닌듯..

profile
Backend Developer

0개의 댓글