Pagini recente » Cod sursa (job #1557374) | Cod sursa (job #2352058) | Cod sursa (job #2407917) | Cod sursa (job #1077816) | Cod sursa (job #2284517)
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
int main()
{int n=2,m=3,nr=0;
int h=5,j=9;
char c[101],s[101];
f.get(c,101);
f.get();
f.get(s,101);
int q=strlen(c);
long long hprin1=0;
long long hprin2=0;
long long hsec1=0;
long long hsec2=0;
for(int i=1;i<=q;i++)
{
hprin1=hprin1+c[i-1]*pow(n,q-i);
hprin1%h%h%h;
hprin2=hprin2+c[i-1]*pow(m,q-i);
hprin2%j%j%j;
}
for(int i=1;i<=q;i++)
{
hsec1=hsec1+s[i-1]*pow(n,q-i);
hsec1%h%h%h;
hsec2=hsec2+s[i-1]*pow(m,q-i);
hsec2%j%j%j;
}
int poz[101];
int p=strlen(s);
for(int i=1;i<p-q+1;i++)
{if(hprin1==hsec1&&hprin2==hsec2)
{
poz[nr++]=i-1;
}
hsec1=hsec1-(s[i-1]*pow(n,q-1));
hsec1=hsec1*n;
hsec1+=s[i+2];
hsec1%h%h%h;
hsec2=hsec2-(s[i-1]*pow(m,q-1));
hsec2=hsec2*m;
hsec2+=s[i+2];
hsec2%j%j%j;
}
g<<nr<<"\n";
for(int i=0;i<nr;i++)
g<<poz[i]<<" ";
return 0;
}