Pagini recente » Cod sursa (job #226602) | Cod sursa (job #246255) | Cod sursa (job #2194571) | Cod sursa (job #880115) | Cod sursa (job #2312235)
#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 k=0;
int p(Trie *t,char *s)
{
if(strlen(s)==0 || t->child[CH(s)]==0) return k;
return k++,p(t->child[CH(s)],s+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':k=0,g<<p(T,s+2)<<'\n'; break;
}
f.get();f.get(s,32);
}
return 0;
}