Pagini recente » Cod sursa (job #3143931) | Cod sursa (job #2466420) | Cod sursa (job #1219539) | Cod sursa (job #1011242) | Cod sursa (job #2665140)
#include <bits/stdc++.h>
#define CH (*ch - 'a')
using namespace std;
struct Trie
{
Trie *alp[26];
int cnt , let;
Trie()
{
cnt = let = 0;
memset(alp , 0 , sizeof(alp));
}
};
Trie *T = new Trie;
char line[30];
void add(Trie *&nod , char *ch)
{
if(*ch == '\0')
return ++nod -> cnt , void();
if(nod -> alp[CH] == NULL)
++nod -> let , nod -> alp[CH] = new Trie;
add(nod -> alp[CH] , ch + 1);
}
bool del(Trie *&nod , char *ch)
{
if(*ch == '\0')
{
--nod -> cnt;
if(!nod -> cnt && !nod -> let && nod != T)
return delete nod , 1;
return 0;
}
bool ok = del(nod -> alp[CH] , ch + 1);
if(ok)
{
nod -> alp[CH] = NULL;
--nod -> let;
if(!nod -> cnt && !nod -> let && nod != T)
return delete nod , 1;
}
return 0;
}
void cnt(Trie *nod , char *ch)
{
if(*ch == '\0')
return cout << nod -> cnt << '\n' , void();
if(nod -> alp[CH] == NULL)
return cout << 0 << '\n' , void();
cnt(nod -> alp[CH] , ch + 1);
}
void precin(Trie *nod , char *ch , int ans)
{
if(*ch == '\0')
return cout << ans << '\n' , void();
if(nod -> alp[CH] == NULL)
return cout << ans << '\n' , void();
precin(nod -> alp[CH] , ch + 1 , ans + 1);
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("trie.in" , "r" , stdin) , freopen("trie.out" , "w" , stdout);
ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
#endif
while(cin.getline(line , 25))
{
if(*line == '0')
add(T , line + 2);
else if(*line == '1')
del(T , line + 2);
else if(*line == '2')
cnt(T , line + 2);
else if(*line == '3')
precin(T , line + 2 , 0);
}
return 0;
}