Cod sursa(job #98214)

Utilizator VmanDuta Vlad Vman Data 10 noiembrie 2007 11:20:21
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 0.94 kb
#include <stdio.h>
#include <string.h>

#define Lmax 10000001
#define Lcmax 21
#define Dim 1048575

char s[Lmax],c[Lcmax];
int hash[Dim];
int L,Lc,tot,i,Nrmax;
long long nr;

int main()
{
 freopen("abc2.in","r",stdin);
 scanf("%s\n",&s);
 L=strlen(s);
 scanf("%s\n",&c);
 Lc=strlen(c);
 Nrmax=(1<<Lc);
 Nrmax=Nrmax*Nrmax-1;
 for (i=0;i<Lc;++i)
      nr=(nr*4)+s[i]-'a';
 ++hash[(nr*16/15) % Dim];
 for (i=1;i<=L-Lc;++i)
     {
      nr=((nr*4)&Nrmax)+s[i+Lc-1]-'a';
      ++hash[(nr*16/15) & Dim];
     }
 for (i=0,nr=0;i<Lc;++i)
     nr=(nr*4)+c[i]-'a';
 tot+=hash[(nr*16/15) & Dim];
 hash[(nr*16/15) & Dim]=0;
 while (! feof(stdin))
       {
        scanf("%s\n",c);
        for (i=0,nr=0;i<Lc;++i)
            nr=(nr*4)+c[i]-'a';
        tot+=hash[(nr*16/15) & Dim];
        hash[(nr*16/15) & Dim]=0;
       }
 freopen("abc2.out","w",stdout);
 printf("%d",tot);
 fclose(stdin);
 fclose(stdout);
 return 0;
}