-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotel_reviews.cpp
More file actions
75 lines (69 loc) · 1.61 KB
/
Hotel_reviews.cpp
File metadata and controls
75 lines (69 loc) · 1.61 KB
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
const int SIZE = 26;
struct trie{
trie* child[SIZE];
bool isLeaf;
};
trie* getNode(){
trie* tmp = new trie();
tmp->isLeaf = false;
for(int i=0;i<SIZE;i++)
tmp->child[i] = NULL;
return tmp;
}
void insert(trie* root, string key){
trie* tmp = root;
for(int i=0;i<key.length();i++){
int idx = key[i]-'a';
if(!tmp->child[idx])
tmp->child[idx] = getNode();
tmp = tmp->child[idx];
}
tmp->isLeaf = true;
}
bool search(trie* root, string key){
trie* tmp = root;
for(int i=0;i<key.length();i++){
int idx = key[i]-'a';
if(!tmp->child[idx])
return false;
tmp = tmp->child[idx];
}
return (tmp!=NULL && tmp->isLeaf);
}
void convert(string& s){
for(int i=0;i<s.size();i++)
if(s[i]=='_')
s[i] = ' ';
return;
}
bool compare(const pair<int,int>& a,const pair<int,int>& b){
if(a.first==b.first)
return a.second<b.second;
return a.first>b.first;
}
vector<int> Solution::solve(string A, vector<string> &B) {
vector<int> res;
vector<pair<int,int>> tmp;
trie* root = getNode();
string word;
stringstream SS;
convert(A);
SS<<A;
while(SS>>word)
insert(root,word);
int count;
for(int i=0;i<B.size();i++){
convert(B[i]);
SS.clear();
SS<<B[i];
count = 0;
while(SS>>word)
if(search(root,word))
count++;
tmp.push_back({count,i});
}
sort(tmp.begin(),tmp.end(),compare);
for(int i=0;i<B.size();i++)
res.push_back(tmp[i].second);
return res;
}