Pagini recente » Cod sursa (job #243776) | Cod sursa (job #720987) | Cod sursa (job #1231476) | Cod sursa (job #3253970) | Cod sursa (job #2651524)
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
vector < int > v[Nmax];
vector < int > copilas[Nmax];
int plod[Nmax];
struct nod
{
char litera;
int cate;
};
nod a[Nmax];
char word[21];
int new_node=1;
void check(int start)
{
// cout<<a[start].litera<<'\n';
for(auto nod : v[start])
{
g<<start<< a[start].litera<<'/'<<a[start].cate<<' '<<nod<<a[nod].litera<<'/'<<a[nod].cate<<'\n';
check(nod);
}
}
void update_plozi_plus(int nod)
{
plod[nod]++;
if(!copilas[nod].empty())
update_plozi_plus(copilas[nod][0]);
}
void update_plozi_minus(int nod)
{
plod[nod]--;
if(!copilas[nod].empty())
update_plozi_plus(copilas[nod][0]);
}
void do0()
{
int where = 0;
int i;
for(i=0; word[i]; i++)
{
bool gata = 0;
bool found = 0;
char litera = word[i];
for(auto candidat : v[where])
{
if(a[candidat].litera == litera)
{
where = candidat;
found = 1;
if(!word[i+1])
{
a[where].cate++;
update_plozi_plus(where);
}
break;
}
}
if(!found)
{
//cout << "2 10";
for(; word[i]; i++)
{
v[where].push_back(new_node);
copilas[new_node].push_back(where);
a[new_node].litera = word [i];
where=new_node;
new_node++;
}
a[where].cate++;
update_plozi_plus(where);
break;
}
}
//g<<"||||||||||||||||||||||||||||||||||||||||||||||\n";
}
void do1()
{
int where = 0;
int i;
for(i=0; word[i]; i++)
{
bool gata = 0;
bool found = 0;
char litera = word[i];
for(auto candidat : v[where])
{
if(a[candidat].litera == litera)
{
where = candidat;
found = 1;
if(!word[i+1])
{
a[where].cate--;
update_plozi_minus(where);
}
break;
}
}
}
}
void do2()
{
int where = 0;
int i;
for(i=0; word[i]; i++)
{
bool gata = 0;
bool found = 0;
char litera = word[i];
for(auto candidat : v[where])
{
if(a[candidat].litera == litera)
{
where = candidat;
found = 1;
if(!word[i+1])
g<< a[where].cate << '\n';
break;
}
}
}
}
void do3()
{
int where = 0;
int i;
int answer=0;
for(i=0; word[i]; i++)
{
bool gata = 0;
bool found = 0;
char litera = word[i];
for(auto candidat : v[where])
{
if(a[candidat].litera == litera)
{
where = candidat;
found = 1;
if(plod[where])answer = i+1;
if(!word[i+1])
{
g<<answer<< '\n';
}
break;
}
}
if(!found)
{
g<<answer <<'\n';
return;
}
}
}
int main(int start)
{
a[0].litera = '*';
int task;
while(f>>task)
{
f>>word;
if(task==0)
do0();
if(task==1)
do1();
if(task==2)
do2();
if(task==3)
do3();
for(int i=0; i<=20; i++)
word[i]='\0';
}
// check(0);
return 0;
}