Pagini recente » Cod sursa (job #890846) | Cod sursa (job #2484580) | Cod sursa (job #2045780) | Cod sursa (job #2073142) | Cod sursa (job #2415614)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
struct trie
{
int nrcuv, nrf;
trie *fii[26];
trie()
{
nrcuv = 0;
nrf = 0;
for (int i = 0; i < 26; i++)
fii[i] = nullptr;
}
};
trie *t = new trie;
void op1(trie *nod, char *s)
{
if (*s == NULL)
nod->nrcuv++;
else
{
if (nod->fii[*s - 'a'] == 0)
{
nod->nrf++;
nod->fii[*s - 'a'] = new trie;
}
op1(nod->fii[*s - 'a'], s + 1);
}
}
int op2(trie *nod, char *s)
{
if (*s == NULL)
nod->nrcuv--;
else if (op2(nod->fii[*s - 'a'], s + 1))
{
nod->nrf--;
nod->fii[*s - 'a'] = 0;
}
if (nod->nrcuv == 0 && nod->nrf == 0 && nod != t)
{
delete nod;
return 1;
}
return 0;
}
int nrap(trie *nod, char *s)
{
if (*s == NULL)
return nod->nrcuv;
else if (nod->fii[*s - 'a'] != 0)
return nrap(nod->fii[*s - 'a'], s + 1);
return 0;
}
int lung(trie *nod, char *s)
{
if (nod->fii[*s - 'a'] == 0 || *s == NULL)
return 0;
return 1 + lung(nod->fii[*s - 'a'], s + 1);
}
char s[30];
int main()
{
int n;
while (fin >> n)
{
fin.getline(s, 30);
if (n == 0)
op1(t, s + 1);
if (n == 1)
op2(t, s + 1);
if (n == 2)
fout << nrap(t, s + 1) << '\n';
if (n == 3)
fout << lung(t, s + 1) << '\n';
}
return 0;
}