Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 16 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Jun 20, 2022
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
21c5983
Update 642.Design-Search-Autocomplete-System.cpp
wisdompeak Jun 19, 2022
8f5c9fb
Update Readme.md
wisdompeak Jun 19, 2022
57e78dd
Create 2311.Longest-Binary-Subsequence-Less-Than-or-Equal-to-K_v1.cpp
wisdompeak Jun 19, 2022
64d401c
Create 2311.Longest-Binary-Subsequence-Less-Than-or-Equal-to-K_v2.cpp
wisdompeak Jun 19, 2022
16bd37e
Update 2311.Longest-Binary-Subsequence-Less-Than-or-Equal-to-K_v1.cpp
wisdompeak Jun 19, 2022
99fca9d
Create Readme.md
wisdompeak Jun 19, 2022
655136c
Update Readme.md
wisdompeak Jun 19, 2022
36cd4a4
Create 2312.Selling-Pieces-of-Wood.cpp
wisdompeak Jun 19, 2022
99bb6dd
Update Readme.md
wisdompeak Jun 19, 2022
0cb6957
Create Readme.md
wisdompeak Jun 19, 2022
54e342b
Update Readme.md
wisdompeak Jun 19, 2022
94b6826
Create 2222.Number-of-Ways-to-Select-Buildings.cpp
wisdompeak Jun 19, 2022
fbf5e9a
Update Readme.md
wisdompeak Jun 19, 2022
99304f3
Create Readme.md
wisdompeak Jun 19, 2022
7d629b3
Update 2222.Number-of-Ways-to-Select-Buildings.cpp
wisdompeak Jun 19, 2022
f3ef226
Update Readme.md
wisdompeak Jun 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update 642.Design-Search-Autocomplete-System.cpp
  • Loading branch information
wisdompeak authored Jun 19, 2022
commit 21c5983222ffa2f7d922f23a6311a23c3039c6c2
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,70 +1,88 @@
class AutocompleteSystem {
unordered_map<string,int>Map;
string data;

struct cmp
class TrieNode
{
public:
TrieNode* next[128];
set<pair<int,string>>top;
TrieNode()
{
bool operator()(pair<string,int>a, pair<string,int>b)
{
if (a.second==b.second)
return a.first<b.first;
else
return a.second>b.second;
}
};
for (int i=0; i<128; i++)
next[i] = NULL;
}
};

class AutocompleteSystem {
TrieNode* root;
string inputStr;
TrieNode* cur;
int flag = 1;
public:
AutocompleteSystem(vector<string> sentences, vector<int> times)
AutocompleteSystem(vector<string>& sentences, vector<int>& times)
{
root = new TrieNode();
cur = root;
for (int i=0; i<sentences.size(); i++)
Map[sentences[i]]=times[i];
data.clear();
add(root, sentences[i], 0, -times[i]);
}

void add(TrieNode* node, const string sentence, int i, int freq)
{
if (i==sentence.size()) return;

int k = sentence[i];
if (node->next[k] == NULL)
node->next[k] = new TrieNode();
node = node->next[k];

int f = 0;
for (auto iter = node->top.begin(); iter!=node->top.end(); iter=next(iter))
{
if (iter->second == sentence)
f = iter->first;
}
if (f!=0) node->top.erase({f, sentence});
node->top.insert(make_pair(f+freq, sentence));

add(node, sentence, i+1, freq);
}

vector<string> input(char c)
{
inputStr.push_back(c);

if (c=='#')
{
Map[data]++;
data.clear();
inputStr.pop_back();
add(root, inputStr, 0, -1);
inputStr = "";
cur = root;
flag = 1;
return {};
}

data.push_back(c);
priority_queue<pair<string,int>,vector<pair<string,int>>,cmp>pq;

for (auto x:Map)
else if (flag==0)
{
string a=x.first;
if (match(data,a))
{
pq.push({a,Map[a]});
if (pq.size()>3) pq.pop();
}
return {};
}

vector<string>results;
while (!pq.empty())
else if (cur->next[c]==NULL)
{
results.push_back(pq.top().first);
pq.pop();
flag = 0;
return {};
}
reverse(results.begin(),results.end());
return results;
}

bool match(string a, string b)
{
for (int i=0; i<a.size(); i++)

cur = cur->next[c];
vector<string>rets;
for (auto iter = cur->top.begin(); iter!=cur->top.end(); iter=next(iter))
{
if (i>=b.size() || a[i]!=b[i])
return false;
rets.push_back(iter->second);
if (rets.size()==3) break;
}
return true;
return rets;

}

};

/**
* Your AutocompleteSystem object will be instantiated and called as such:
* AutocompleteSystem obj = new AutocompleteSystem(sentences, times);
* vector<string> param_1 = obj.input(c);
* AutocompleteSystem* obj = new AutocompleteSystem(sentences, times);
* vector<string> param_1 = obj->input(c);
*/

AltStyle によって変換されたページ (->オリジナル) /