Pagini recente » Istoria paginii runda/jujitsu | Rating Iorgulescu Matei (matei1404014) | Statistici FMI Honceriu Mihai (miahi) | Cod sursa (job #561452) | Cod sursa (job #2137382)
#include <bits/stdc++.h>
#define chr s[0]-'a'
using namespace std;
struct Trie{
int nrsons, words;
Trie *sons[26];
Trie (){
nrsons=0;
words=0;
for (int i=0;i<26;i++)
sons[i]=NULL;
}
};
Trie *T= new Trie;
void ins(Trie *nod, char *s)
{
if (!s[0])
{
nod->words++;
return;
}
if (nod->sons[chr]==NULL)
{
nod->sons[chr]=new Trie;
nod->nrsons++;
}
ins(nod->sons[chr], s+1);
}
bool del(Trie *nod, char*s)
{
if (!s[0])
nod->words--;
else
{
if (del(nod->sons[chr], s+1))
{
nod->sons[chr]=0;
nod->nrsons--;
}
}
return 0;
}
int cnt(Trie *nod, char *s)
{
if (!s[0])
return nod->words;
else
{
if (!nod->sons[chr])
return 0;
return cnt(nod->sons[chr], s+1);
}
}
int lcpref(Trie *nod, char*s, int niv)
{
if (!s[0])
return niv;
else
{
if (!nod->sons[chr])
return niv;
return lcpref(nod->sons[chr], s+1, niv+1);
}
}
int main()
{
ifstream cin("trie.in");
ofstream cout ("trie.out");
ios_base::sync_with_stdio(0); cin.tie(0);
int op;
char cuv[21];
while (cin>>op>>cuv)
{
switch (op)
{
case 0: ins(T, cuv);
break;
case 1: del(T, cuv);
break;
case 2: cout<<cnt(T, cuv)<<'\n';
break;
case 3: cout<<lcpref(T, cuv, 0)<<'\n';
break;
}
}
return 0;
}