Pagini recente » Cei mai harnici utilizatori info-arena | Borderou de evaluare (job #387777) | Borderou de evaluare (job #2247758) | Cod sursa (job #2656177) | Cod sursa (job #2313903)
#include<fstream>
#include<cstring>
using namespace std;
struct T
{
char c;
int n,p,s,b;
}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].s; nod; nod = t[nod].b)
if (t[nod].c == w[p])
{
t[nod].p++;
if (p == leng - 1)
{
t[nod].n++;
return;
}
root = nod;
find = true;
break;
}
else br = nod;
if (find) continue;
t[++z].c = w[p];
if (p == leng - 1) t[z].n = t[z].p = 1;
else t[z].n = 0, t[z].p = 1;
if (! t[root].s) t[root].s = z;
else t[br].b = z;
root = z;
}
}
void D()
{
int root = 0, leng = strlen (w);
for (int i = 1; i < leng; ++ i)
for (int nod = t[root].s; nod; nod = t[nod].b)
if (t[nod].c == w[i])
{
t[nod].p--;
if (i == leng - 1) t[nod].n --;
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].s; nod; nod = t[nod].b)
if (t[nod].c == w[i] && t[nod].p)
{
if (i == leng - 1) return t[nod].n;
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].s; nod; nod = t[nod].b)
if (t[nod].c == w[i] && t[nod].p)
{
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';
}
}