fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. using namespace std;
  4. #include <string>
  5.  
  6. #define SIZE 26
  7. //TrieTree
  8. struct TrieTree {
  9. struct Node {
  10. Node* children[SIZE];
  11. bool isEndOfWord;
  12. string translation;
  13. };
  14.  
  15. Node* root;
  16.  
  17. Node* createNode() {
  18. Node* node = new Node;
  19. node->isEndOfWord = false;
  20. for (int i = 0; i < SIZE; i++)
  21. node->children[i] = NULL;
  22. return node;
  23. }
  24.  
  25. void insert(string word, string translation) {
  26. Node* tmpNode = root;
  27. for (int i = 0; i < word.length(); i++) {
  28. int index = word[i] - 'a';
  29. if (tmpNode->children[index] == NULL)
  30. tmpNode->children[index] = createNode();
  31.  
  32. tmpNode = tmpNode->children[index];
  33. }
  34. tmpNode->isEndOfWord = true;
  35. tmpNode->translation = translation;
  36. }
  37.  
  38. string search(string key) {
  39. Node* tmpNode = root;
  40. for (int i = 0; i < key.length(); i++) {
  41. int index = key[i] - 'a';
  42. if (tmpNode->children[index] == NULL)
  43. return "-";
  44.  
  45. tmpNode = tmpNode->children[index];
  46. }
  47. return tmpNode->translation;
  48. }
  49.  
  50. };
  51.  
  52.  
  53.  
  54. int main() {
  55.  
  56. TrieTree T;
  57. T.root = T.createNode();
  58. /*T.insert("kot", "cat");
  59.   cout << T.search("kot");
  60.   T.insert("pies", "dog");
  61.   cout << T.search("pies");
  62.   T.insert("mysz", "mouse");
  63.   cout << T.search("mysz");
  64.   T.insert("piesek", "doggy");
  65.   cout << T.search("piesek");
  66.   T.insert("myszka", "mousy");
  67.   cout << T.search("myszka");
  68.   T.insert("kotek", "citty");
  69.   cout << T.search("kotek");*/
  70.  
  71. //while (true) {
  72. // char command;
  73. // string word, translation;
  74. // scanf("%s", command);
  75. // if (feof(stdin) != 0) break;
  76.  
  77. // if (command == '+') {
  78. // cin >> word;
  79. // cin >> translation;
  80. // T.insert(word, translation);
  81.  
  82. // }
  83. // if (command == '?') {
  84. // cin >> word;
  85. // cout << T.search(word);
  86. // }
  87. // if (command == '*') {
  88.  
  89. // }
  90. //}
  91.  
  92. char command;
  93. while (cin>>command) {
  94. string word, translation;
  95.  
  96. if (command == '+') {
  97. cin >> word;
  98. cin >> translation;
  99. T.insert(word, translation);
  100.  
  101. }
  102. if (command == '?') {
  103. cin >> word;
  104. cout << T.search(word);
  105. }
  106. if (command == '*') {
  107.  
  108. }
  109. }
  110.  
  111. }
  112.  
  113.  
Success #stdin #stdout 0.01s 5520KB
stdin
+ dog pies
+ cat kot
+ do robic
+ doggie piesek
+ doggies pieski
+ dolphin delfin
+ ant mrowka
+ zzz xxx
? dog
? cat
? do
? doggie
? dolphin
? ant
? zzz
? d
? dot
? dom
? dogg
? dol
? dolp
? dolph
? dolphi
? dolphine
stdout
pieskotrobicpiesekdelfinmrowkaxxx---