Pagini recente » Cod sursa (job #2412768) | Cod sursa (job #1898130) | Cod sursa (job #2230857) | Cod sursa (job #2951280) | Cod sursa (job #2313898)
#include<fstream>
#include<cstring>
using namespace std;
struct T
{
char car;
int NrW,Pref,son,bro;
}t[5200026];
char w[25];
int z,o;
void A()
{
int root = 0, br = 0;
int leng = strlen (w);
for (int p = 1; p < leng; ++ p)
{
bool find = false;
for (int nod = t[root].son; nod; nod = t[nod].bro)
if (t[nod].car == w[p])
{
t[nod].Pref ++;
if (p == leng - 1)
{
t[nod].NrW ++;
return;
}
root = nod;
find = true;
break;
}
else br = nod;
if (find) continue;
t[++z].car = w[p];
if (p == leng - 1) t[z].NrW = t[z].Pref = 1;
else t[z].NrW = 0, t[z].Pref = 1;
if (! t[root].son) t[root].son = z;
else t[br].bro = z;
root = z;
}
}
void D()
{
int root = 0, leng = strlen (w);
for (int i = 1; i < leng; ++ i)
for (int nod = t[root].son; nod; nod = t[nod].bro)
if (t[nod].car == w[i])
{
t[nod].Pref --;
if (i == leng - 1) t[nod].NrW --;
root = nod;
break;
}
}
int C()
{
int root = 0, leng = strlen (w);
bool ok = true;
for (int i = 1; i < leng && ok; ++ i)
{
ok = false;
for (int nod = t[root].son; nod; nod = t[nod].bro)
if (t[nod].car == w[i] && t[nod].Pref)
{
if (i == leng - 1) return t[nod].NrW;
root = nod;
ok = true;
break;
}
}
return 0;
}
int M()
{
int res = 0, leng = strlen (w), root = 0;
bool ok = true;
for (int i = 1; i < leng && ok; ++ i)
{
ok = false;
for (int nod = t[root].son; nod; nod = t[nod].bro)
if (t[nod].car == w[i] && t[nod].Pref)
{
res ++;
root = nod, ok = true;
break;
}
}
return res;
}
int main()
{
ifstream f("trie.in");
ofstream g("trie.out");
while(f>>o)
{
f.getline(w,25);
if(!o)
A();
else if(o==1)
D();
else if(o==2)
g<<C()<<'\n';
else
g<<M()<<'\n';
}
}