|
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() |
6 | 7 | { |
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; |
15 | 18 | public: |
16 | | - AutocompleteSystem(vector<string> sentences, vector<int> times) |
| 19 | + AutocompleteSystem(vector<string>& sentences, vector<int>& times) |
17 | 20 | { |
| 21 | + root = new TrieNode(); |
| 22 | + cur = root; |
18 | 23 | 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); |
21 | 46 | } |
22 | 47 |
|
23 | 48 | vector<string> input(char c) |
24 | 49 | { |
| 50 | + inputStr.push_back(c); |
| 51 | + |
25 | 52 | if (c=='#') |
26 | 53 | { |
27 | | - Map[data]++; |
28 | | - data.clear(); |
| 54 | + inputStr.pop_back(); |
| 55 | + add(root, inputStr, 0, -1); |
| 56 | + inputStr = ""; |
| 57 | + cur = root; |
| 58 | + flag = 1; |
29 | 59 | return {}; |
30 | 60 | } |
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) |
36 | 62 | { |
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 {}; |
43 | 64 | } |
44 | | - |
45 | | - vector<string>results; |
46 | | - while (!pq.empty()) |
| 65 | + else if (cur->next[c]==NULL) |
47 | 66 | { |
48 | | - results.push_back(pq.top().first); |
49 | | - pq.pop(); |
| 67 | + flag = 0; |
| 68 | + return {}; |
50 | 69 | } |
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)) |
58 | 74 | { |
59 | | - if (i>=b.size() || a[i]!=b[i]) |
60 | | - return false; |
| 75 | + rets.push_back(iter->second); |
| 76 | + if (rets.size()==3) break; |
61 | 77 | } |
62 | | - return true; |
| 78 | + return rets; |
| 79 | + |
63 | 80 | } |
| 81 | + |
64 | 82 | }; |
65 | 83 |
|
66 | 84 | /** |
67 | 85 | * 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); |
70 | 88 | */ |
0 commit comments