Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <set>
  5. #include <algorithm>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. struct tree
  11. {
  12. map<char, tree*> next;
  13. set<string> words;
  14. tree()
  15. {
  16. for (int i = 0; i < 26; i++)
  17. next[i] = NULL;
  18. }
  19. }root;
  20.  
  21. void find_words(string s)
  22. {
  23. tree* current = &root;
  24. cout << s << endl;
  25. for (int i = 0; i < s.size(); i++)
  26. {
  27. if (current->next[s[i]] == NULL)
  28. return;
  29. current = current->next[s[i]];
  30. }
  31. cout << s << endl;
  32. for (auto i : current->words)
  33. cout << "." << i << endl;
  34. }
  35.  
  36. void add_word(string s)
  37. {
  38. tree* current = &root;
  39. for (int i = 0; i < s.size(); i++)
  40. {
  41. if (current->next[s[i]] == NULL)
  42. current->next[s[i]] = new tree();
  43. current->words.insert(s);
  44. if (current->words.size() > 20)
  45. current->words.erase(--(current->words.end()));
  46. current = current->next[s[i]];
  47. }
  48. }
  49.  
  50. int main()
  51. {
  52. string s;
  53. add_word("sun");
  54. while(cin >> s)
  55. {
  56. char com = s[0];
  57. s = s.substr(1, s.size() - 1);
  58. if (com == '?')
  59. find_words(s);
  60. else
  61. add_word(s);
  62. /*cin >> s;*/
  63. }
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement