Pagini recente » Cod sursa (job #3247978) | Cod sursa (job #2597310) | Cod sursa (job #798802) | Cod sursa (job #285693) | Cod sursa (job #2881549)
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())
using ll = long long;
const string fn = "trie";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");
char s[100000];
const int SIGMA = 26;
class TrieNode {
public:
int fr;
int cnt;
TrieNode *nxt[SIGMA];
TrieNode() {
fr = cnt = 0;
for (int i = 0; i < SIGMA; ++i)
nxt[i] = NULL;
}
void ins(char *w) {
if (*w == 0)
this->cnt++;
else {
if (this->nxt[*w - 'a'] == NULL) {
this->nxt[*w - 'a'] = new TrieNode;
}
this->nxt[*w - 'a']->fr++;
this->nxt[*w - 'a']->ins(w + 1);
}
}
void del(char *w) {
if (*w == 0) {
this->cnt--;
}
else {
this->nxt[*w - 'a']->fr--;
this->nxt[*w - 'a']->del(w + 1);
}
}
int nrap(char *w) {
if (*w == 0) {
return this->cnt;
}
else {
if (this->nxt[*w - 'a'] == NULL)
return 0;
return this->nxt[*w - 'a']->nrap(w + 1);
}
}
int lcp(char *w) {
if (*w == 0) {
return 0;
}
else {
if (this->nxt[*w - 'a'] == NULL)
return 0;
if (this->nxt[*w - 'a']->fr == 0)
return 0;
return 1 + this->nxt[*w - 'a']->lcp(w + 1);
}
}
};
class Trie {
public:
TrieNode *root;
Trie() {
this->root = new TrieNode;
}
void insert(char *w) {
this->root->ins(w);
}
void del(char *w) {
this->root->del(w);
}
int nrap(char *w) {
this->root->nrap(w);
}
int lcp(char *w) {
this ->root->lcp(w);
}
} t;
int main() {
int op;
while (fin >> op >> s) {
if (op == 0)
t.insert(s);
if (op == 1)
t.del(s);
if (op == 3)
fout << t.nrap(s) << '\n';
if (op == 4)
fout << t.lcp(s) << '\n';
}
return 0;
}