Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <set>
- #include <map>
- using namespace std;
- struct Trie {
- map<char,Trie*> next;
- set<string> words;
- }root;
- void find_words(string s) {
- Trie* current = &root;
- cout << s << '\n';
- for (int i = 0; i < s.size(); ++i) {
- if (current->next[s[i]] == NULL)
- return;
- current = current->next[s[i]];
- }
- for (auto i : current->words)
- cout << " " << i << '\n';
- }
- void add_word(string s) {
- Trie* current = &root;
- for (int i = 0; i < s.size(); ++i) {
- if (current->next[s[i]] == NULL)
- current->next[s[i]] = new Trie();
- current->words.insert(s);
- if (current->words.size() > 20)
- current->words.erase(--(current->words.end()));
- current = current->next[s[i]];
- }
- current->words.insert(s);
- }
- int main() {
- string s;
- add_word("sun");
- while (cin >> s) {
- char comm = s[0];
- s = s.substr(1, s.size() - 1);
- if (comm == '?')
- find_words(s);
- else
- add_word(s);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement