Skip to content

Commit b1e2c96

Browse files
committed
[2019kakao-p6] 매칭 점수
1 parent 1c73e0a commit b1e2c96

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
| 01 | | [오픈채팅방](./src/_2019_KAKAO_BLIND_RECRUITMENT/P1) | |
266266
| 02 | | [실패율](./src/_2019_KAKAO_BLIND_RECRUITMENT/P2) | |
267267
| 03 | | [후보키](./src/_2019_KAKAO_BLIND_RECRUITMENT/P3) | |
268+
| 06 | | [매칭 점수](./src/_2019_KAKAO_BLIND_RECRUITMENT/P6) | |
268269

269270
---
270271

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package _2019_KAKAO_BLIND_RECRUITMENT.P6;
2+
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
Solution sol = new Solution();
8+
System.out.println(sol.solution("blind", new String[]{"<html lang=\"ko\" xml:lang=\"ko\" xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n <meta charset=\"utf-8\">\n <meta property=\"og:url\" content=\"https://a.com\"/>\n</head> \n<body>\nBlind Lorem Blind ipsum dolor Blind test sit amet, consectetur adipiscing elit. \n<a href=\"https://b.com\"> Link to b </a>\n</body>\n</html>", "<html lang=\"ko\" xml:lang=\"ko\" xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n <meta charset=\"utf-8\">\n <meta property=\"og:url\" content=\"https://b.com\"/>\n</head> \n<body>\nSuspendisse potenti. Vivamus venenatis tellus non turpis bibendum, \n<a href=\"https://a.com\"> Link to a </a>\nblind sed congue urna varius. Suspendisse feugiat nisl ligula, quis malesuada felis hendrerit ut.\n<a href=\"https://c.com\"> Link to c </a>\n</body>\n</html>", "<html lang=\"ko\" xml:lang=\"ko\" xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n <meta charset=\"utf-8\">\n <meta property=\"og:url\" content=\"https://c.com\"/>\n</head> \n<body>\nUt condimentum urna at felis sodales rutrum. Sed dapibus cursus diam, non interdum nulla tempor nec. Phasellus rutrum enim at orci consectetu blind\n<a href=\"https://a.com\"> Link to a </a>\n</body>\n</html>"}));
9+
// output 0
10+
System.out.println(sol.solution("Muzi", new String[]{"<html lang=\"ko\" xml:lang=\"ko\" xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n <meta charset=\"utf-8\">\n <meta property=\"og:url\" content=\"https://careers.kakao.com/interview/list\"/>\n</head> \n<body>\n<a href=\"https://programmers.co.kr/learn/courses/4673\"></a>#!MuziMuzi!)jayg07con&&\n\n</body>\n</html>", "<html lang=\"ko\" xml:lang=\"ko\" xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n <meta charset=\"utf-8\">\n <meta property=\"og:url\" content=\"https://www.kakaocorp.com\"/>\n</head> \n<body>\ncon%\tmuzI92apeach&2<a href=\"https://hashcode.co.kr/tos\"></a>\n\n\t^\n</body>\n</html>"}));
11+
// output 1
12+
}
13+
}
14+
15+
class Solution {
16+
17+
private static HashMap<String, PageInfo> map;
18+
19+
public int solution(String word, String[] pages) {
20+
int idx = 0;
21+
map = new HashMap<>();
22+
for (String html : pages) {
23+
PageInfo page = new PageInfo(idx++, html.toLowerCase());
24+
page.calBasicScore(word.toLowerCase());
25+
map.put(page.url, page);
26+
}
27+
28+
for (PageInfo page : map.values()) {
29+
page.calMatchScore();
30+
}
31+
32+
ArrayList<PageInfo> list = new ArrayList<>(map.values());
33+
Collections.sort(list);
34+
35+
return list.get(0).index;
36+
}
37+
38+
static class PageInfo implements Comparable<PageInfo> {
39+
int index;
40+
String html;
41+
String url;
42+
String[] linkTo;
43+
int basic = 0;
44+
double match = 0;
45+
46+
public PageInfo(int index, String html) {
47+
this.index = index;
48+
this.html = html;
49+
this.findUrl();
50+
this.linkTo = this.extractLink();
51+
}
52+
53+
private void findUrl() {
54+
int left = 0, mid = 0, right = 0;
55+
while (mid <= left) {
56+
left = this.html.indexOf("<meta", left + 1);
57+
right = this.html.indexOf(">", left);
58+
mid = this.html.lastIndexOf("https://", right);
59+
}
60+
right = this.html.indexOf("\"", mid);
61+
this.url = this.html.substring(mid, right);
62+
}
63+
64+
private String[] extractLink() {
65+
final String LINK_START_TAG = "<a href=\"";
66+
StringBuilder sb = new StringBuilder();
67+
int link_idx = this.html.indexOf(LINK_START_TAG);
68+
while (link_idx >= 0) {
69+
String linkAfter = this.html.substring(link_idx + LINK_START_TAG.length());
70+
sb.append(linkAfter.split("\"")[0]).append(" ");
71+
link_idx = this.html.indexOf(LINK_START_TAG, link_idx + 1);
72+
}
73+
return sb.toString().split(" ");
74+
}
75+
76+
public void calBasicScore(String word) {
77+
int w_idx = 0;
78+
for (int i = 0; i < this.html.length(); i++) {
79+
char c = this.html.charAt(i);
80+
if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) {
81+
if (w_idx < word.length() && c == word.charAt(w_idx)) w_idx ++;
82+
else w_idx = 0;
83+
} else {
84+
if (w_idx == word.length()) this.basic ++;
85+
w_idx = 0;
86+
}
87+
}
88+
if (w_idx == word.length()) this.basic ++;
89+
}
90+
91+
public void calMatchScore() {
92+
this.match += this.basic;
93+
for (String link : this.linkTo) {
94+
if (map.containsKey(link))
95+
map.get(link).match += (double) this.basic / this.linkTo.length;
96+
}
97+
}
98+
99+
@Override
100+
public int compareTo(PageInfo target) {
101+
if (this.match == target.match) {
102+
return this.index - target.index;
103+
} else {
104+
return Double.compare(target.match, this.match);
105+
}
106+
}
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [2019 KAKAO BLIND RECRUITMENT - 6] 매칭 점수
2+
3+
![image](https://user-images.githubusercontent.com/22045163/103168140-fd1e6a80-4873-11eb-9d0e-a19039399782.png)
4+
5+
죽는 줄

0 commit comments

Comments
 (0)