Pagini recente » Cod sursa (job #1219152) | Cod sursa (job #597797) | Cod sursa (job #2130126) | Cod sursa (job #2359778) | Cod sursa (job #1003504)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
char s1[2000005],s2[2000005];
int n,m,hash1,hash2,x1,x2,mod1=100007,mod2=100021,pr=73,ap[2000005],k=0;
int main()
{ int i;
f>>s1>>s2;
m=strlen(s1); n=strlen(s2);
if (m>n) {g<<"0\n"; return 0;}
for(i=0;i<m;i++)
{ hash1=(hash1+s1[i]*pr)%mod1;
hash2=(hash2+s1[i]*pr)%mod2; }
for(i=0;i<n;i++)
{ x1=(x1+pr*s2[i])%mod1;
x2=(x2+pr*s2[i])%mod2;
if (i+1>m)
{ x1=(x1-pr*s2[i-m])%mod1;
if (x1<0) x1+=mod1;
x2=(x2-pr*s2[i-m])%mod2;
if (x2<0) x2+=mod2;
if (x1==hash1 && x2==hash2)
{ k++; ap[k]=i-m+1;}
}
}
g<<k<<"\n";
for(i=1;i<=k;i++) g<<ap[i]<<" ";
return 0;
}