Cod sursa(job #1481820)

Utilizator SilviuIIon Silviu SilviuI Data 5 septembrie 2015 12:34:27
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <stdio.h>
#include <cstring>
#include <vector>
#define lmax 10000010
#define hmod 666013
#define mod 99991
#define p 257
using namespace std;
int n,i,m,p1,sol=0;
unsigned int curenthash;
char s[lmax],ss[30];
vector <int> uhash[mod+10];
bool inhash(int x)
{
    int y=x%mod;
    for (unsigned int i=0;i<uhash[y].size();i++)
        if (uhash[y][i]==x) return true;
    return false;
}
int main() {
freopen("abc2.in","r",stdin);
freopen("abc2.out","w",stdout);
gets(s+1); n=strlen(s+1);
while (gets(ss+1)>0) {
    m=strlen(ss+1); curenthash=0;
    for (i=1;i<=m;i++) curenthash=(curenthash*p+ss[i])%hmod;
    if (!inhash(curenthash)) uhash[curenthash%mod].push_back(curenthash);
}
curenthash=0; p1=1;
for (i=1;i<=m;i++) {
    curenthash=(curenthash*p+s[i])%hmod;
    if (i>1) p1=(p1*p)%hmod;
}
sol=sol+inhash(curenthash);
for (i=m+1;i<=n;i++) {
    curenthash=(((curenthash-(s[i-m]*p1)%hmod)+hmod)*p+s[i])%hmod;
    sol=sol+inhash(curenthash);
}
printf("%d",sol);
return 0;
}