Pagini recente » Cod sursa (job #1978612) | Cod sursa (job #1871985) | Cod sursa (job #2126939) | Cod sursa (job #1842182) | Cod sursa (job #101419)
Cod sursa(job #101419)
#include<stdio.h>
#include<string.h>
//typedef struct nod {nod* c[3];} nod;
typedef struct nod {nod* c[3];unsigned char n;bool used;} nod;
nod *p,*q[21];
char N[30];
long nr;
nod* go(nod* q,char a)
{if(!q->c[a-'a'])
{q->c[a-'a']=new nod;
q=q->c[a-'a'];
q->c[0]=q->c[1]=q->c[2]=0;
q->n=1;
q->used=0;
return q;}
q=q->c[a-'a'];
q->n=q->n+1;
return q;}
void build_arbore_solutii()
{q[0]=p;
char c;int i,j;
scanf("%c",&c);
for(i=1;i<=20&&c>='a'&&c<'d';i++)
{for(j=i;j>0;j--)
q[j]=go(q[j-1],c);
scanf("%c",&c);}
while(c>='a'&&c<'d')
{for(j=20;j>0;j--)
q[j]=go(q[j-1],c);
scanf("%c",&c);}}
int count()
{int n=strlen(N),i;
nod*q=p;
for(i=0;i<n;i++)
if(!q->c[N[i]-'a']) return 0;
else q=q->c[N[i]-'a'];
if(q->used) return 0;
q->used=1;
return q->n;}
int main()
{freopen("abc2.in","r",stdin);
freopen("abc2.out","w",stdout);
// p=new nod;p->c[1]=p->c[0]=p->c[2]=0;
p=new nod;p->c[0]=p->c[1]=p->c[2]=0;
build_arbore_solutii();
while(!feof(stdin))
{scanf(" %s ",N);
nr=nr+count();
}
printf("%ld",nr);
fclose(stdout);
return 0;}