Pagini recente » Cod sursa (job #21269) | Cod sursa (job #1878831) | Cod sursa (job #1540219) | Cod sursa (job #2150958) | Cod sursa (job #471752)
Cod sursa(job #471752)
#include<fstream>
#include<queue>
using namespace std;
#define FIN "abc2.in"
#define FOUT "abc2.out"
#define NMAX 10000003
#define LMAX 31
struct trie
{
bool T;
trie *NEXT[3];
trie* UP;
trie()
{
T = false;
NEXT[0] = NULL;
NEXT[1] = NULL;
NEXT[2] = NULL;
UP = NULL;
}
};
typedef trie* Nod;
Nod ROOT;
int N,rez = 0 ;
char SIR[NMAX];
char TEMP[31];
queue<Nod> Q;
void insert( Nod &nod , char* s )
{
if ( *s == '\n' )
{
nod -> T = true;
return;
}
if( !nod -> NEXT[*s - 'a']) nod -> NEXT[*s - 'a'] = new trie;
insert(nod -> NEXT[*s - 'a'] , s + 1 );
}
void construct()
{
ROOT -> UP = ROOT;
for(int i = 0 ; i <= 2 ; ++i)
if( ROOT -> NEXT[i] ) Q.push(ROOT -> NEXT[i]) , ROOT -> NEXT[i] -> UP = ROOT;
while(! Q.empty())
{
Nod aux = Q.front();
Q.pop();
for(int i = 0 ;i <= 2 ; ++i)
if( aux -> NEXT[i])
{
Q.push(aux -> NEXT[i]);
Nod v = aux -> UP;
while( !v -> NEXT[i] && v != ROOT ) v = v -> UP;
if(v -> NEXT [i] == NULL) aux -> NEXT[i] -> UP = ROOT;
else aux -> NEXT[i] -> UP = v -> NEXT[i];
}
}
for(int i = 0 ; i <= 2 ; ++i)
if( ROOT -> NEXT[i] ) Q.push(ROOT -> NEXT[i]);
else ROOT -> NEXT[i] = ROOT;
while(!Q.empty())
{
Nod aux = Q.front();
Q.pop();
for(int i = 0 ; i <= 2 ; ++i)
if( aux -> NEXT[i] ) Q.push(aux -> NEXT[i]);
else {
Nod tm = aux -> UP;
while( !tm -> NEXT[i]) tm = tm -> UP;
aux -> NEXT[i] = tm -> NEXT[i];
}
}
}
int solve()
{
int poz = 0;
Nod act = ROOT;
while(SIR[poz] != '\n')
{
act = act -> NEXT[SIR[poz] - 'a'] ;
poz++;
if(act -> T) rez ++;
}
return rez;
}
int main()
{
ifstream in(FIN);
ofstream out(FOUT);
in.getline(SIR , NMAX , '\n');
int tmp = strlen(SIR);
SIR[tmp] = '\n';
ROOT = new trie;
while(!in.eof())
{
in.getline(TEMP,LMAX,'\n');
tmp = strlen(TEMP);
TEMP[tmp] = '\n';
insert(ROOT , TEMP);
}
construct();
out<<solve();
return 0;
}