Cod sursa(job #100348)

Utilizator anna_bozianuBozianu Ana anna_bozianu Data 12 noiembrie 2007 08:44:07
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.37 kb
#include<stdio.h>
#include<string.h>
struct nod{
nod *a;
nod *b;
nod *c;
};
char txt[10000005],cuv[25],*p,*q,*aux;
size_t lt,lc,i;
long int cont;
nod *rad;
void insert(char *pp,nod *rr);
long int search(char *pp,nod *rr);
int main()
{
	FILE *f,*g;f=fopen("abc2.in","r");g=fopen("abc2.out","w");
	fscanf(f,"%s",txt);lt=strlen(txt);
	rad =new nod;rad->a=0;rad->b=0;rad->c=0;
	fscanf(f,"%s",cuv);lc=strlen(cuv);insert(cuv,rad);
	while(fscanf(f,"%s",cuv)!=EOF)insert(cuv,rad);
	p=txt;q=p+lc;
	for(i=0;i<=lt-lc;i++)
	{ *aux=*q;
	  *q=0;
	  cont+=search(p,rad);
	  *q=*aux;
	  p++;
	  q++;
	}
	fprintf(g,"%ld\n",cont);
	fcloseall();
	return 0;
}
void insert(char *pp,nod *rr)
{       nod *rrr;
	if(*pp=='a')
	 { if(!rr->a)
	   { rrr=new nod;rrr->a=0;rrr->b=0;rrr->c=0;rr->a=rrr;}
	   insert(pp+1,rr->a);
	   return;
	 }
	 if(*pp=='b')
	 { if(!rr->b)
	   { rrr=new nod;rrr->a=0;rrr->b=0;rrr->c=0;rr->b=rrr;}
	   insert(pp+1,rr->b);
	   return;
	 }
	 if(*pp=='c')
	 { if(!rr->c)
	   { rrr=new nod;rrr->a=0;rrr->b=0;rrr->c=0;rr->c=rrr;}
	   insert(pp+1,rr->c);
	   return;
	 }
}
long int search(char *pp,nod *rr)
{
	if(*pp=='a')
	{ if(!rr->a) return 0;
	  return search(pp+1,rr->a);
	}
	if(*pp=='b')
	{ if(!rr->b) return 0;
	  return search(pp+1,rr->b);
	}
       if(*pp=='c')
	{ if(!rr->c) return 0;
	  return search(pp+1,rr->c);
	}
	return 1;
}