Skip to content

Commit 21c5983

Browse files
authored
Update 642.Design-Search-Autocomplete-System.cpp
1 parent ce439a6 commit 21c5983

1 file changed

Lines changed: 64 additions & 46 deletions

File tree

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,88 @@
1-
class AutocompleteSystem {
2-
unordered_map<string,int>Map;
3-
string data;
4-
5-
struct cmp
1+
class TrieNode
2+
{
3+
public:
4+
TrieNode* next[128];
5+
set<pair<int,string>>top;
6+
TrieNode()
67
{
7-
bool operator()(pair<string,int>a, pair<string,int>b)
8-
{
9-
if (a.second==b.second)
10-
return a.first<b.first;
11-
else
12-
return a.second>b.second;
13-
}
14-
};
8+
for (int i=0; i<128; i++)
9+
next[i] = NULL;
10+
}
11+
};
12+
13+
class AutocompleteSystem {
14+
TrieNode* root;
15+
string inputStr;
16+
TrieNode* cur;
17+
int flag = 1;
1518
public:
16-
AutocompleteSystem(vector<string> sentences, vector<int> times)
19+
AutocompleteSystem(vector<string>& sentences, vector<int>& times)
1720
{
21+
root = new TrieNode();
22+
cur = root;
1823
for (int i=0; i<sentences.size(); i++)
19-
Map[sentences[i]]=times[i];
20-
data.clear();
24+
add(root, sentences[i], 0, -times[i]);
25+
}
26+
27+
void add(TrieNode* node, const string sentence, int i, int freq)
28+
{
29+
if (i==sentence.size()) return;
30+
31+
int k = sentence[i];
32+
if (node->next[k] == NULL)
33+
node->next[k] = new TrieNode();
34+
node = node->next[k];
35+
36+
int f = 0;
37+
for (auto iter = node->top.begin(); iter!=node->top.end(); iter=next(iter))
38+
{
39+
if (iter->second == sentence)
40+
f = iter->first;
41+
}
42+
if (f!=0) node->top.erase({f, sentence});
43+
node->top.insert(make_pair(f+freq, sentence));
44+
45+
add(node, sentence, i+1, freq);
2146
}
2247

2348
vector<string> input(char c)
2449
{
50+
inputStr.push_back(c);
51+
2552
if (c=='#')
2653
{
27-
Map[data]++;
28-
data.clear();
54+
inputStr.pop_back();
55+
add(root, inputStr, 0, -1);
56+
inputStr = "";
57+
cur = root;
58+
flag = 1;
2959
return {};
3060
}
31-
32-
data.push_back(c);
33-
priority_queue<pair<string,int>,vector<pair<string,int>>,cmp>pq;
34-
35-
for (auto x:Map)
61+
else if (flag==0)
3662
{
37-
string a=x.first;
38-
if (match(data,a))
39-
{
40-
pq.push({a,Map[a]});
41-
if (pq.size()>3) pq.pop();
42-
}
63+
return {};
4364
}
44-
45-
vector<string>results;
46-
while (!pq.empty())
65+
else if (cur->next[c]==NULL)
4766
{
48-
results.push_back(pq.top().first);
49-
pq.pop();
67+
flag = 0;
68+
return {};
5069
}
51-
reverse(results.begin(),results.end());
52-
return results;
53-
}
54-
55-
bool match(string a, string b)
56-
{
57-
for (int i=0; i<a.size(); i++)
70+
71+
cur = cur->next[c];
72+
vector<string>rets;
73+
for (auto iter = cur->top.begin(); iter!=cur->top.end(); iter=next(iter))
5874
{
59-
if (i>=b.size() || a[i]!=b[i])
60-
return false;
75+
rets.push_back(iter->second);
76+
if (rets.size()==3) break;
6177
}
62-
return true;
78+
return rets;
79+
6380
}
81+
6482
};
6583

6684
/**
6785
* Your AutocompleteSystem object will be instantiated and called as such:
68-
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
69-
* vector<string> param_1 = obj.input(c);
86+
* AutocompleteSystem* obj = new AutocompleteSystem(sentences, times);
87+
* vector<string> param_1 = obj->input(c);
7088
*/

0 commit comments

Comments
 (0)