aboutsummaryrefslogtreecommitdiff
path: root/semestr-3/pf/lista7/zaj.cpp
blob: 403754fc5faaab205de8aa0becb3747a0106fe71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;

const int ALPHABET_SIZE = 26;

struct node
{
  node *children[ALPHABET_SIZE];
  int cnt;
  bool is_end;

  node()
  {
    for (int i = 0; i < ALPHABET_SIZE; i++)
      children[i] = nullptr;
    is_end = false;
    cnt = 0;
  }
};

struct TRIE
{
  node *root;

  TRIE()
  {
    root = new node;
  }

  // dodaje słowo do słownika
  void add_word(string word)
  {
    node *crawl = root;
    for (auto c : word)
    {
      int letter = c - 'a';
      if (crawl->children[letter] == nullptr)
      {
        crawl->children[letter] = new node;
      }
      crawl = crawl->children[letter];
    }
    crawl->is_end = true;
  }

  // sprawdź czy słowo jest w słowniku
  bool find(string word)
  {
    node *crawl = root;
    for (auto c : word)
    {
      int letter = c - 'a';
      if (crawl->children[letter] == nullptr)
        return false;
      crawl = crawl->children[letter];
    }
    return crawl->is_end;
  }

  // usuń słowo ze słownika
  void erase(string word) {}
};

int main()
{
  TRIE dictionary;
  int q;
  cin >> q;
  while (q--)
  {
    char c;
    string s;
    cin >> c >> s;
    if (c == 'A')
      dictionary.add_word(s);
    else
      cout << (dictionary.find(s) ? "TAK" : "NIE")
           << "\n";
  }
}