Cod sursa(job #101636)

Utilizator fireatmyselfBogdan-Alexandru Stoica fireatmyself Data 13 noiembrie 2007 17:52:00
Problema Abc2 Scor 0
Compilator c Status done
Runda Happy Coding 2007 Marime 1.06 kb
#include <stdio.h>
#include <string.h>
#define S ((1<<20)-1)

int V[S+10], Nr;
char ss[10000002], ss2[50002];

int main()
{
        int i, j, hash, l, n, t;

        freopen("abc2.in", "r", stdin);
        gets(ss);
        n = strlen(ss);
        scanf(" ");
        gets(ss2);
        l = strlen(ss2);
        n = n-l;

        for (i = 0; i <= n; i++)
        {
            hash = 65599;
            for (j = 0; j < l; j++)
                hash = (ss[i+j]+(hash<<6)+(hash<<16)+t)&S, t = -hash;
            V[hash]++;
        }

        hash = 65599;
        for (j = 0; j < l; j++)
            hash = (ss2[j]+(hash<<6)+(hash<<16)-hash)&S;
        Nr += V[hash];
        V[hash] = 0;

        while (scanf(" %s", ss2)==1)
        {
                hash = 65599;
                for (j = 0; j < l; j++)
                    hash = (ss2[j]+(hash<<6)+(hash<<16)-hash)&S;
                Nr += V[hash];
                V[hash] = 0;
        }

        freopen("abc2.out", "w", stdout);
        printf("%d\n", Nr);

        return 0;
        
}