Nu aveti permisiuni pentru a descarca fisierul grader_sirul2.cpp
Cod sursa(job #497396)
| Utilizator | Data | 2 noiembrie 2010 13:11:38 | |
|---|---|---|---|
| Problema | Abc2 | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 1.13 kb |
#include <stdio.h>
#include <string.h>
int n,m,nr,sol,pow,i,M;
struct hash
{
int nod;
hash *link;
}*H[100000];
char str[10000005],s[22];
inline void add(int x)
{
hash *p;
p=new hash;
p->nod=x;
p->link=H[x%M];
H[x%M]=p;
}
inline int src(int x)
{
hash *p;
p=H[x%M];
while(p!=NULL)
{
if(p->nod==x) return 1;
p=p->link;
}
return 0;
}
int main()
{
freopen("abc2.in","r",stdin);
freopen("abc2.out","w",stdout);
fgets(str,10000005,stdin);
M=39967;
n=strlen(str)-1;
if(str[n]=='\n') n--;
fgets(s,22,stdin);
m=strlen(s)-1;
if(s[m]=='\n') m--;
sol=0;
for(i=0;i<=m;i++)
sol=((sol*101)%M+s[i])%M;
if(src(sol)==0) add(sol);
while(!feof(stdin))
{
fgets(s,25,stdin);
if(!feof(stdin))
{
sol=0;
for(i=0;i<=m;i++)
sol=((sol*101)%M+s[i])%M;
if(src(sol)==0) add(sol);
}
}
sol=0;
pow=1;
nr=0;
for(i=1;i<=m;i++)
pow=(pow*101)%M;
for(i=0;i<=m;i++)
sol=((sol*101)%M+str[i])%M;
if(src(sol)) nr++;
for(i=m+1;i<=n;i++)
{
sol=((sol+M-(pow*str[i-m-1])%M)*101+str[i])%M;
if(src(sol)) nr++;
}
printf("%d\n",nr);
return 0;
}
