Pagini recente » Cod sursa (job #2820816) | Cod sursa (job #2152699) | Cod sursa (job #2231497) | Cod sursa (job #2561349) | Cod sursa (job #1218204)
#include<fstream>
#include<vector>
#include<cstring>
using namespace std;
struct cell
{
int frecv;
bool ok;
cell *e[30];
cell()
{
ok=0;
frecv=0;
for (int i=1;i<=26;i++) e[i]=NULL;
}
};
ifstream fin("trie.in");
ofstream fout("trie.out");
const int oo=1<<30;
int sol;
char s[30];
cell *root=new cell;
inline void ADAUGA(char *t,cell *p,int i,int len)
{
if (p->e[t[i]-'a'+1]==NULL) p->e[t[i]-'a'+1]=new cell;
p->e[t[i]-'a'+1]->frecv=p->e[t[i]-'a'+1]->frecv +1;
if (i==len) p->e[t[i]-'a'+1]->ok=1;
if (i<len) ADAUGA(t,p->e[t[i]-'a'+1],i+1,len);
}
inline void STERGE(char *t,cell *p,int i,int len,cell *q)
{
if (i==len)
{
p->frecv=p->frecv-1;
if (p->frecv==0) {p->ok=0;q->e[t[i]-'a'+1]=NULL;}
return ;
}
STERGE(t,p->e[t[i+1]-'a'+1],i+1,len,p);
p->frecv=p->frecv-1;
if (p->frecv==0) q->e[t[i]-'a'+1]=NULL;
}
inline int COUNT(char *t,cell *p,int i,int len)
{
if (p->e[t[i]-'a'+1]==NULL || (i==len && p->e[t[i]-'a'+1]->ok==0) ) {sol=0;return 0;}
sol=min(sol,p->e[t[i]-'a'+1]->frecv);
if (i<len) COUNT(t,p->e[t[i]-'a'+1],i+1,len);
return sol;
}
inline void PREFIX(char *t,cell *p,int i,int len)
{
if (i==len+1) fout<<i<<"\n";
else
if (p->e[t[i]-'a'+1]==NULL) fout<<i-1<<"\n";
else PREFIX(t,p->e[t[i]-'a'+1],i+1,len);
}
inline void SOLVE()
{
int ok;
while (fin>>ok)
{
fin>>(s+1);
if (!ok) ADAUGA(s,root,1,strlen(s+1));
if (ok==1) STERGE(s,root->e[s[1]-'a'+1],1,strlen(s+1),root);
if (ok==2) {sol=oo;fout<<COUNT(s,root,1,strlen(s+1))<<"\n";}
if (ok==3) PREFIX(s,root,1,strlen(s+1));
}
}
int main()
{
SOLVE();
return 0;
}