Pagini recente » Cod sursa (job #1748797) | Cod sursa (job #1260916) | Cod sursa (job #1724282) | Cod sursa (job #723162) | Cod sursa (job #1725712)
#include <fstream>
#include <vector>
#define h 1003
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
string a,b;
int A, B, i, j, cnt, ap[1010], ha, hb, x=3, m=16113, u, v, putere(int, int);
int main()
{
f>>a>>b;
A=a.size();
B=b.size();
for(i=0, j=1; i<A; i++,j=(j*x)%m)
{
ha=(ha+a[i]*j)%m;
hb=(hb+b[i]*j)%m;
}
if(ha==hb)
{
cnt++;
if(cnt<=1000)
ap[cnt]=0;
}
u=putere(x, m-2);
v=putere(x, A-1);
for(j=0,i=A; i<B; i++,j++)
{
hb=((hb+m-b[j])*u+v*b[i])%m;
if(ha==hb)
{
cnt++;
if(cnt<=1000)
ap[cnt]=j+1;
}
}
g<<cnt<<"\n";
j=min(1000, cnt);
for(i=1; i<=j; i++)
g<<ap[i]<<' ';
return 0;
}
int putere(int b, int e)
{
int r = 1;
for(;e;e>>=1)
{
if(e&1)
r=(r*b)%m;
b=(b*b)%m;
}
return r;
}