Cod sursa(job #749992)
#include <fstream>
#include <cstring>
#define cifra T[poz]-'a'
#define LE 50005
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct two
{
int litere,link,okz;
} trie[LE][30];
string T;
int nNew;
int cLink;
int Push(int semn)
{
int poz=0;
int lung=T.length()-1;
int lungime=0;
while (poz<=lung)
{
trie[cLink][cifra].litere+=semn;
if (poz==lung)
{
trie[cLink][cifra].okz+=semn;
break;
}
if (semn==0)
if (trie[cLink][cifra].litere==0)
{
--poz;
break;
}
if (trie[cLink][cifra].link==0&&semn==1)
{
trie[cLink][cifra].link=nNew+1;
cLink=++nNew;
}
else
cLink=trie[cLink][cifra].link;
++poz;
}
return poz;
}
int main()
{
int tip=33;
int last;
while ( f>>tip)
{
cLink=0;
f>>T;
if(tip==0)
{
Push(1);
}
if (tip==1)
Push(-1);
if (tip==2)
{
if (Push(0)==T.length()-1)
{
last=T[T.length()-1]-'a';
g<<trie[cLink][last].okz;
}
else g<<0;
g<<'\n';
}
if (tip==3)
g<<Push(0)+1<<'\n';
}
f.close();
g.close();
return 0;
}