-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign-search-autocomplete-system.cpp
More file actions
71 lines (61 loc) · 1.88 KB
/
Copy pathdesign-search-autocomplete-system.cpp
File metadata and controls
71 lines (61 loc) · 1.88 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
class Node{
public:
unordered_map<char,Node*> um;
int time=0;
};
// ??????????????wrong answer??????
class AutocompleteSystem {
public:
AutocompleteSystem(vector<string>& sentences, vector<int>& times) {
for(int i=0;i<sentences.size();i++){
auto cur=root;
for(const auto &c:sentences[i]){
if(cur->um.find(c)==cur->um.end()) cur->um[c]=new Node();
cur=cur->um[c];
}
cur->time=times[i];
}
}
vector<string> input(char c) {
if(c=='#'){
now->time++;
now=root;
tmp="";
return {}; // ???# ,???????????
}
if(now->um.find(c)==now->um.end()){
now->um[c]=new Node(); // ?????????????????
now=now->um[c];
return {};
}
vector<pair<int,string>> cand; // ?????????????????????????????
now=now->um[c];
tmp+=c;
dfs(now,cand,tmp);
auto cmp=[](pair<int,string> &a, pair<int,string> &b){
if(a.first!=b.first) return a.first>b.first;
else return a.second<b.second;
};
sort(cand.begin(),cand.end(),cmp);
vector<string> res;
for(int i=0;i<min(3,(int)cand.size());i++) res.push_back(cand[i].second);
return res;
}
void dfs(Node *root, vector<pair<int,string>> &cand, string tmp){
if(root->time!=0){
cand.push_back(make_pair(root->time,tmp));
}
for(auto &[c,n]:root->um){
dfs(n,cand,tmp+c);
}
}
// ????????
Node *root=new Node();
string tmp="";
Node* now=root;
};
/**
* Your AutocompleteSystem object will be instantiated and called as such:
* AutocompleteSystem* obj = new AutocompleteSystem(sentences, times);
* vector<string> param_1 = obj->input(c);
*/