Pagini recente » Cod sursa (job #2499332) | Istoria paginii runda/rudna_2_star | Cod sursa (job #1783517) | Cod sursa (job #1143478) | Cod sursa (job #2312232)
#include <bits/stdc++.h>
#define CH(s) (*s - 'a')
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct Trie
{
int cnt, nch;
Trie *child[26];
Trie() {
cnt=nch=0;
memset(child,0,sizeof(child));
}
};
Trie *T = new Trie;
void i(Trie *t,char *s)
{
if(strlen(s)==0) { t->cnt++; return; }
if(t->child[CH(s)]==0)
t->child[CH(s)]=new Trie, t->nch++;
i(t->child[CH(s)],s+1);
}
int d(Trie *t,char *s)
{
if(strlen(s)==0) t->cnt--;
else if(d(t->child[CH(s)],s+1))
t->child[CH(s)]=0,t->nch--;
if(t->cnt==0 && t->nch==0 && t!=T ) { delete t; return 1; }
return 0;
}
int q(Trie *t,char *s)
{
if(strlen(s)==0) return t->cnt;
if(t->child[CH(s)])
return q(t->child[CH(s)],s+1);
return 0;
}
int p(Trie *t,char *s,int k)
{
if(strlen(s)==0 || t->child[CH(s)]==0) return k;
return p(t->child[CH(s)],s+1,k+1);
}
int main() {
char s[32];
f.get(s,32);
while(!f.eof())
{
switch(s[0])
{
case '0':i(T,s+2); break;
case '1':d(T,s+2); break;
case '2':g<<q(T,s+2)<<'\n'; break;
case '3':g<<p(T,s+2,0)<<'\n'; break;
}
f.get();f.get(s,32);
}
return 0;
}