-
Notifications
You must be signed in to change notification settings - Fork 1
/
매칭 점수.cpp
271 lines (217 loc) · 7.72 KB
/
매칭 점수.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// 매칭 점수
// https://programmers.co.kr/learn/courses/30/lessons/42893
// 시행착오
// 1. "/>" 문자열로 탐색 시, 앞에 공백이 있는 경우가 있으므로 "\"" 문자열로 탐색함
// int to = page.find("/>", from); (오류)
// int to = page.find("\"", from + 1) + 1; (수정)
// 2. 문자열 탐색 위치 버그
// int end = body.find(">"); (오류)
// int end = body.find(">", start); (수정)
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <map>
#include <algorithm>
//#include "Print.h"
using namespace std;
void ChangeCapital(string& str)
{
for (char& c : str)
{
if ('A' <= c && c <= 'Z')
{
c = c - 'A' + 'a';
}
}
}
std::vector<std::string> SplitWhitespace(const std::string& str)
{
using namespace std;;
vector<string> results;
istringstream iss(str);
for (string s; iss >> s;)
{
results.push_back(s);
}
return results;
}
std::vector<std::string> SplitNoneAlphabet(const string& str)
{
vector<string> split;
std::string::const_iterator from = begin(str);
std::string::const_iterator to = from;
while (true)
{
to = std::find_if(from, end(str), [](char c)
{
return !('a' <= c && c <= 'z');
});
string substr = string(from, to);
if (substr.size() > 0)
{
split.push_back(substr);
}
if (to == end(str))
break;
from = to + 1;
}
return split;
}
string FindUrl(const string& page)
{
static const string kProp = "<meta property=\"og:url\" content=";
int from = page.find(kProp) + kProp.size();
int to = page.find("\"", from + 1) + 1;
int size = to - from;
string url = page.substr(from, size);
return url;
}
string FindBody(const string& page)
{
static const string kStartBody = "<body>";
static const string kEndBody = "</body>";
int start_idx = page.find(kStartBody) + kStartBody.size();
int end_idx = page.find(kEndBody, start_idx);
int size = end_idx - start_idx;
string body = page.substr(start_idx, size);
return body;
}
vector<string> FindLinks(const string& page, string& body)
{
static const string kStartLink = "<a href=";
static const string kEndLink = "</a>";
vector<string> links;
body = FindBody(page);
while (true)
{
int start_ref = body.find(kStartLink);
if (start_ref == std::string::npos)
break;
int start = start_ref + kStartLink.size();
int end = body.find(">", start);
int size = end - start;
string link = body.substr(start, size);
links.push_back(link);
// remove
int ref_end = body.find(kEndLink) + kEndLink.size();
int ref_size = ref_end - start_ref;
string ref = body.substr(start_ref, ref_size);
auto n = body.find(ref);
if (n != std::string::npos)
{
body.erase(n, ref.length());
}
}
return links;
}
vector<string> FindWords(const string& page, const string& modified_body)
{
vector<string> v = SplitWhitespace(modified_body);
vector<string> words;
for (string& str : v)
{
ChangeCapital(str);
vector<string> split = SplitNoneAlphabet(str);
words.insert(end(words), begin(split), end(split));
}
return words;
}
struct Data
{
Data(const string& url, const vector<string>& links, const vector<string>& words, int index, const string& word)
:
url(url), links(links), words(words), index(index), score(0), link_score(0)
{
for (const string& w : words)
{
if (w == word)
{
score++;
}
}
}
float MatchingScore() const { return (float)score + link_score; }
float PartLinkScore() const { return (float)score / (float)links.size(); }
void Print() const
{
cout << url << endl;
cout << "score : " << score << " link count : " << links.size() << " partlink score : " << PartLinkScore() << " link_score : " << link_score << " matching score : " << MatchingScore() << endl;
for (auto link : links)
{
cout << link << endl;
}
cout << endl;
for (auto word : words)
{
cout << word << endl;
}
cout << endl;
}
string url;
vector<string> links;
vector<string> words;
int index;
int score;
float link_score;
};
int solution(string word, vector<string> pages)
{
map<string, Data> datamap;
ChangeCapital(word);
for (int i = 0; i < pages.size(); ++i)
{
string page = pages[i];
string url = FindUrl(page);
string modified_body;
vector<string> links = FindLinks(page, modified_body);
vector<string> words = FindWords(page, modified_body);
Data data(url, links, words, i, word);
datamap.insert(make_pair(url, data));
}
for (pair<const string, Data>& e : datamap)
{
Data& data = e.second;
for (const string& link : data.links)
{
auto it = datamap.find(link);
if (it != end(datamap))
{
it->second.link_score += data.PartLinkScore();
}
}
}
vector<Data> vec;
for (auto d : datamap)
{
//d.second.Print();
vec.push_back(d.second);
}
sort(begin(vec), end(vec), [](const Data& a, const Data& b)
{
if (a.MatchingScore() == b.MatchingScore())
{
return a.index < b.index;
}
return a.MatchingScore() > b.MatchingScore();
});
return vec[0].index;
}
int main()
{
string word = "blind";
vector<string> pages = {
"<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>" };
/*
string word = "Muzi";
vector<string> pages = {
"<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>" };
*/
int answer = solution(word, pages);
cout << answer;
return 0;
}