Pagini recente » Cod sursa (job #1733194) | Cod sursa (job #2740801) | Cod sursa (job #2264661) | Cod sursa (job #377000) | Cod sursa (job #3141767)
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct nod {
int nrAp, pr;
nod* next[27];
nod() {
pr = nrAp = 0;
memset(next, 0, sizeof(next));
}
}*T;
char s[25];
int op, n;
void adaug() {
nod *q = T;
q->pr++;
for (int i = 0; i < n; ++i) {
if (q->next[s[i]] == 0) {
q->next[s[i]] = new nod;
}
q = q->next[s[i]];
q->pr++;
}
q->nrAp++;
}
void del() {
nod *q = T;
q->pr--;
for (int i = 0; i < n; ++i) {
q = q->next[s[i]];
q->pr--;
}
q->nrAp--;
}
int prefix() {
nod *q = T;
for (int i = 0; i < n; ++i) {
if (q->next[s[i]] == 0 || q->next[s[i]]->pr == 0) {
return i;
}
q = q->next[s[i]];
}
return n;
}
int cuv() {
nod *q = T;
for (int i = 0; i < n; ++i) {
if (q->next[s[i]] == 0) {
return 0;
}
q = q->next[s[i]];
}
return q->nrAp;
}
int main()
{
T = new nod;
while (f >> op) {
f.get();
f.getline(s, 20);
n = strlen(s);
for (int i = 0; i < n; ++i) {
s[i] -= 'a';
}
if (op == 0) {
adaug();
} else if (op == 1) {
del();
} else if (op == 2) {
g << cuv() << '\n';
} else {
g << prefix() << '\n';
}
}
return 0;
}