Pagini recente » Cod sursa (job #2294338) | Cod sursa (job #470649) | Cod sursa (job #54191) | Cod sursa (job #1552510) | Cod sursa (job #2787402)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
typedef long long ll;
string A,B;
const int prime=73,mod1=100007,mod2=100021,maxn=2e6+5;
ll hashA1=0,hashA2=0,power1=1,power2=1,match[maxn];
void init()
{
fin>>A>>B;
for(ll i=0;i<A.size();i++)
{
hashA1=(hashA1*prime+A[i])%mod1;
hashA2=(hashA2*prime+A[i])%mod2;
if(i!=0)
power1=(power1*prime)%mod1,
power2=(power2*prime)%mod2;
}
}
void rollHash(ll &Hash,ll power,ll mod,ll index)
{
Hash=((Hash-(B[index-A.size()]*power)%mod+mod)*prime+B[index])%mod;
}
void RabinKarp()
{
init();
if(A.size()>B.size())
{
fout<<'0';
return ;
}
ll hash1=0,hash2=0;
for(ll i=0;i<A.size();i++)
{
hash1=(hash1*prime+B[i])%mod1;
hash2=(hash2*prime+B[i])%mod2;
}
int nr=0;
if(hash1==hashA1 && hash2==hashA2)
match[nr++]=0;
for(int i=A.size();i<B.size();i++)
{
rollHash(hash1,power1,mod1,i);
rollHash(hash2,power2,mod2,i);
if(hash1==hashA1 && hash2==hashA2)
match[nr++]=i-A.size()+1;
}
fout<<nr<<"\n";
for(int i=0;i<nr && i<1000;i++)
fout<<match[i]<<" ";
}
int main()
{
RabinKarp();
return 0;
}