Pagini recente » Cod sursa (job #1642640) | Cod sursa (job #1178568) | Cod sursa (job #2903052) | Cod sursa (job #757975) | Cod sursa (job #1654898)
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
fstream fin("trie.in",ios::in),fout("trie.out",ios::out);
string s;
int maxi;
struct tr
{
int trm,ct;//cate cuvinte se termina,cate cuvinte trec prin nodul curent
tr* nxt[26];
//cout<<"unu\n";
tr()
{
//cout<<"doi\n";
trm=ct=0;
memset(nxt,0,sizeof(nxt));
}
};
void in_out(tr* n,int p,int op)
{
//(*n).ct++;
if(op==0)
(n->ct)++;
if(op==1)
(n->ct)--;
if(op==3 && (n->ct))
maxi=max(maxi,p);
if(p==s.size())
{
if(op==0)
(n->trm)++;
if(op==1)
(n->trm)--;
if(op==2)
fout<<(n->trm)<<"\n";
if(op==3)
fout<<maxi<<"\n";
return ;
}
int nx=s[p]-'a';
if(n->nxt[nx]==NULL)
{
n->nxt[nx]=new tr;
}
in_out(n->nxt[nx],p+1,op);
}
int main()
{
int type;
tr* t=new tr;
while(fin>>type)
{
fin>>s;
maxi=-9;
in_out(t,0,type);
}
}