반응형

문제

https://www.welcomekakao.com/learn/courses/30/lessons/42893#



점수 계산식은 간단하나 풀이 중 혼동이 올 수 있으니 미리 정리해 두는 것이 좋습니다. 이 문제는 점수 계산 보다는 <meta>태그와 <a>태그를 찾아내는 것과 대문자, 소문자 구분없이 검색어가 몇번 처리됬는지 찾아내는 것이 중요합니다. 구현은 자바로 진행했습니다.



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 
import java.util.HashMap;
import java.util.HashSet;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
    private static final Pattern URL_PATTERN = Pattern.compile("<meta property=\"og:url\" content=\"https://(.+?)\"/>");
    private static final Pattern EXTERNAL_LINK = Pattern.compile("<a href=\"https://(.+?)\">");
    private HashMap<String, Page> pageMap = new HashMap<>();
 
    private class Page {
        private HashSet<String> hadExternalLinks = new HashSet<>();
        private HashSet<String> externalLinks = new HashSet<>();
        private String html;
        private String word;
        private String url;
        private int defaultScore = 0;
        public int id;
 
        public Page(String word, String html, int id) {
            this.word = word;
            this.html = html;
            this.id = id;
            initUrl();
            initDefaultScore();
        }
 
        private void initDefaultScore() {
            int find = html.indexOf(word);
            while (find != -1)
            {
                Character[] wordBorder = { html.charAt(find - 1), html.charAt(find + word.length()) };
                if (Arrays.stream(wordBorder).anyMatch(ch -> ch.charValue() >= 'a' && ch.charValue() <= 'z'== false)
                    defaultScore++;
                find = html.indexOf(word, find + 1);
            }
        }
 
        private void initUrl() {
            Matcher matcher = URL_PATTERN.matcher(html);
            while (matcher.find())
                url = matcher.group(1);
        }
 
        public double getTotalScore() {
            double ret = defaultScore;
            for (String link : externalLinks) {
                if (pageMap.containsKey(link)) {
                    Page externalPage = pageMap.get(link);
                    if (externalPage.hadExternalLinks.size() > 0)
                        ret += (double) externalPage.defaultScore / externalPage.hadExternalLinks.size();
                }
            }
            return ret;
        }
 
        public void initExternalLink() {
            Matcher matcher = EXTERNAL_LINK.matcher(html);
            while (matcher.find()) {
                String link = matcher.group(1);
                if (hadExternalLinks.contains(link) == false)
                    hadExternalLinks.add(link);
                if (pageMap.containsKey(link))
                    pageMap.get(link).externalLinks.add(url);
            }
        }
    }
 
    public int solution(String word, String[] pages) {
        int id = 0;
        for (String html : pages) {
            Page page = new Page(word.toLowerCase(), html.toLowerCase(), id);
            pageMap.put(page.url, page);
            id++;
        }
        for (Page page : pageMap.values()) {
            page.initExternalLink();
        }
        return pageMap.values().stream().max((a, b) -> Double.compare(a.getTotalScore(), b.getTotalScore())).get().id;
    }
}
cs


반응형

'Algorithm' 카테고리의 다른 글

[BOJ 2839] 설탕배달  (0) 2018.12.18
[BOJ 5543] 상근날드  (0) 2018.12.18
[프로그래머스] K번째 수  (0) 2018.11.14
[BOJ 2293] 동전 1  (0) 2018.11.12
[BOJ 2309] 일곱 난쟁이  (0) 2018.11.12

+ Recent posts