# Cod sursa(job #1787540)

Utilizator Data 24 octombrie 2016 19:43:35 Potrivirea sirurilor 40 cpp done Arhiva educationala 1.29 kb
``````#include <fstream>
#include <string.h>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
char A[2000002],B[2000002];
int h1,h2,g1,g2,p1,p2,i,c,poz[2000002],n,m,M1,M2,baza;
int main()
{
baza=73;
M1=100007;
M2=100021;
fin>>A>>B;
n=strlen(A);
m=strlen(B);
if(n>m)
{
fout<<0;
return 0;
}
h1=0;
h2=0;
for(i=0;i<=n-1;i++)
{
h1=(h1*baza+A[i])%M1;
h2=(h2*baza+A[i])%M2;
}
p1=1;
p2=1;
for(i=1;i<=n-1;i++)
{
p1=(p1*baza)%M1;
p2=(p2*baza)%M2;
}
c=0;
g1=0;
g2=0;
for(i=0;i<=n-1;i++)
{
g1=(g1*baza+B[i])%M1;
g2=(g2*baza+B[i])%M2;
}
if(h1==g1 && h2==g2)
{
c++;
poz[c]=0;
}
for(i=1;i+n-1<=m-1;i++)
{
//sterg din g1 si g2 pe B[i-1]
//apoi adaugam in g1 si g2 B[i+n-1]
g1=(((g1-B[i-1]*p1%M1+M1)%M1)*baza+B[i+n-1])%M1;
g2=(((g2-B[i-1]*p2%M2+M2)%M2)*baza+B[i+n-1])%M2;
if(h1==g1 && h2==g2)
{
c++;
poz[c]=i;
}
}
if(c>1000)
{
c=1000;
}
fout<<c<<'\n';
for(i=1;i<=c;i++)
{
fout<<poz[i]<<" ";
}

return 0;
}
``````