Pagini recente » Cod sursa (job #1136922) | Cod sursa (job #2602250) | Cod sursa (job #3185182) | Cod sursa (job #3239986) | Cod sursa (job #1582333)
#include <fstream>
#include <cstring>
using namespace std;
struct Node{
bool Ex;
Node *next[3];
Node () { Ex = next[0] = next[1] = next[2] = 0; }
}* Root;
char Main[10000031];
char Word[22];
void Input();
void Insert(Node*, char*);
bool Existence(Node*, char*);
int main()
{
Input();
int sol = 0;
for (char *i = Main; *i != '\0'; ++i)
sol += Existence(Root, i);
ofstream os ("abc2.out");
os << sol;
os.close();
}
void Input()
{
ifstream is ("abc2.in");
is >> Main;
for ( Root = new Node(); is >> Word; Insert(Root, Word));
is.close();
};
void Insert(Node* x, char* w)
{
if (*w == '\0')
{
x->Ex = 1;
return;
}
if (x->next[*w-'a'] == 0)
x->next[*w-'a'] = new Node();
Insert(x->next[*w-'a'], w+1);
};
bool Existence(Node* x, char* w)
{
if (*w == '\0')
return x->Ex;
if(x->Ex == 1)
return 1;
if (x->next[*w-'a'] == 0)
return 0;
return Existence(x->next[*w-'a'], w+1);
};