Pagini recente » Cod sursa (job #758102) | Cod sursa (job #1996619) | Cod sursa (job #142095) | Cod sursa (job #1877337) | Cod sursa (job #2436918)
#include <bits/stdc++.h>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
int tip,n,j;
char s[100];
struct trie
{
int nr,nrcuv;
vector <int> v;
trie()
{
nr=nrcuv=0;
v.resize(26);
}
};
vector <trie> arb;
void update()
{
int nod=0;
n=strlen(s);
for (j=0;j<n;j++)
{
if (arb[nod].v[s[j]-'a']==0)
{
arb.push_back(trie());
arb[nod].v[s[j]-'a']=arb.size()-1;
}
nod=arb[nod].v[s[j]-'a'];
arb[nod].nr++;
}
arb[nod].nrcuv++;
}
void sterge()
{
int nod=0;
n=strlen(s);
for (j=0;j<n;j++)
{
nod=arb[nod].v[s[j]-'a'];
arb[nod].nr--;
}
arb[nod].nrcuv--;
}
int nrcuv()
{
int nod=0;
n=strlen(s);
for (j=0;j<n;j++)
{
nod=arb[nod].v[s[j]-'a'];
if (arb[nod].nr==0)return 0;
}
return arb[nod].nrcuv;
}
int celmailungprefix()
{
int nod=0;
n=strlen(s);
for (j=0;j<n;j++)
{
nod=arb[nod].v[s[j]-'a'];
if (arb[nod].nr==0)
{
return j;
}
}
return n;
}
int main()
{
int nod=0;
arb.push_back(trie());
while (f>>tip>>s)
{
if (tip==0)
{
update();
}
else
if (tip==1)
{
sterge();
}
else
if (tip==2)
{
g<<nrcuv()<<'\n';
}
else
{
g<<celmailungprefix()<<'\n';
}
}
return 0;
}