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