Pagini recente » Cod sursa (job #2425194) | Cod sursa (job #2093443) | Cod sursa (job #1929026) | Cod sursa (job #1146495) | Cod sursa (job #2385030)
#include <fstream>
#include <string.h>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
char s1[2000000],s2[2000000];
unsigned long long hsh1,hsh2,sz1,sz2,x=1,y=1,i,hsh12,hsh22,nr;
int sol[2000];
int main()
{
f>>s1>>s2;
sz1=strlen(s1);
sz2=strlen(s2);
for(i=0; i<sz1; i++)
{
hsh1=hsh1*33+s1[i];
hsh2=hsh2*73+s1[i];
}
for(i=0; i<sz1; i++)
{
hsh12=hsh12*33+s2[i];
hsh22=hsh22*73+s2[i];
x=x*33;
y=y*73;
}
if(hsh12==hsh1&&hsh22==hsh2) {nr++;sol[nr]=0;}
for(i=sz1; i<sz2; i++)
{
hsh12=hsh12*33-s2[i-sz1]*x+s2[i];
hsh22=hsh22*73-s2[i-sz1]*y+s2[i];
if(hsh12==hsh1&&hsh22==hsh2) {nr++;if(nr<=1000)sol[nr]=i-sz1+1;}
}
g<<nr<<'\n';
for(i=1;i<=nr && i<=1000;i++) g<<sol[i]<<' ';
return 0;
}