-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotelReviews.cpp
69 lines (51 loc) · 1.3 KB
/
hotelReviews.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
#include <bits/stdc++.h>
using namespace std;
//Trie
class tn{
public:
tn *next[26];
bool end;
tn(){
for(int i = 0; i < 26; ++i) next[i] = NULL;
end = false;
}
};
void insert(tn *root, string word){
tn *cur = root;
for(int i = 0; i < word.length(); ++i){
if(cur->next[word[i]-'a'] == NULL) cur->next[word[i]-'a'] = new tn();
cur = cur->next[word[i]-'a'];
}
cur->end = true;
}
int good_count(tn*root, string review){
int cnt = 0;
tn* cur = root;
stringstream s(review);
string word;
while(getline(s, word, '_')){
for(int i = 0; i < word.length(); ++i){
if(!cur->next[word[i]-'a']) break;
else cur = cur->next[word[i]-'a'];
}
if(cur->end) ++cnt;
}
return cnt;
}
vector<int> solve(string s, vector<string> &b) {
tn* root = new tn();
vector<pair<int, int>> v;
stringstream ss(s);
string cur;
while(getline(ss, cur, '_')) insert(root, cur);
for(int i = 0; i < b.size(); ++i){
string review = b[i];
int cnt = good_count(root, review);
//cout << i << " " << cnt << " \n";
v.emplace_back((-1*cnt), i);
}
sort(v.begin(), v.end());
vector<int> ans;
for(auto p : v) ans.push_back(p.second);
return ans;
}