Pagini recente » Cod sursa (job #414318) | Cod sursa (job #1697529) | Cod sursa (job #570920) | Cod sursa (job #3246354) | Cod sursa (job #1436301)
#include <stdio.h>
#include <cstring>
#define p 54
#define mod1 666013
#define mod2 7654321
#define nmax 2000010
using namespace std;
int n,i,j,m,hash1,hash2,sol,curenthash1,curenthash2,soll[nmax],p11,p22;
char s[nmax],ss[nmax];
int main(){
freopen("strmatch.in","r",stdin);
freopen("strmatch.out","w",stdout);
gets(s+1); gets(ss+1);
hash1=0; hash2=0; curenthash1=0; curenthash2=0; p11=p22=1;
n=strlen(s+1); m=strlen(ss+1);
for (i=1;i<=n;i++) {
hash1=(hash1*p+s[i])%mod1,
hash2=(hash2*p+s[i])%mod2;
if (i>1) {
p11=(p11*p)%mod1;
p22=(p22*p)%mod2;
}
}
if (n>m) { printf("0"); return 0; }
sol=0;
for (i=1;i<=n;i++) curenthash1=(curenthash1*p+ss[i])%mod1,
curenthash2=(curenthash2*p+ss[i])%mod2;
if (curenthash1==hash1 && curenthash2==hash2) sol=1,soll[1]=0;
for (i=n+1;i<=m;i++){
curenthash1=((curenthash1-(ss[i-n]*p11)%mod1+mod1)*p+ss[i])%mod1;
curenthash2=((curenthash2-(ss[i-n]*p22)%mod2+mod2)*p+ss[i])%mod2;
if (curenthash1==hash1 && curenthash2==hash2) {
sol++;
if (sol<=1000) soll[sol]=i-n;
}
}
printf("%d\n",sol);
if (sol>1000) sol=1000;
for (i=1;i<=sol;i++) printf("%d ",soll[i]);
return 0;
}