Pagini recente » Cod sursa (job #1771208) | Cod sursa (job #1830589) | Cod sursa (job #2000282) | Cod sursa (job #1567924) | Cod sursa (job #1617498)
#include <cstring>
#include <fstream>
#include <iostream>
#define P 73
#define mod1 100007
#define mod2 100021
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
char a[2000001], b[2000001];
int na, nb;
int hasha1, hasha2, P1, P2;
char match[2000001];
int main()
{
fin.getline(a,2000001);
fin.getline(b,2000001);
na = strlen(a);
nb = strlen(b);
P1 = P2 = 1;
hasha1 = hasha2 = 0;
for (int i = 0; i < na; i++)
{
hasha1 = (hasha1 * P + a[i]) % mod1;
hasha2 = (hasha2 * P + a[i]) % mod2;
if (i != 0)
P1 = (P1 * P) % mod1,
P2 = (P2 * P) % mod2;
}
if (na > nb)
{
fout<<"0"<<'\n';
return 0;
}
int hash1 = 0, hash2 = 0;
for (int i = 0; i < na; i++)
hash1 = (hash1 * P + b[i]) % mod1,
hash2 = (hash2 * P + b[i]) % mod2;
int nr = 0;
if (hash1 == hasha1 && hash2 == hasha2)
match[0] = 1, nr++;
for (int i = na; i < nb; i++)
{
hash1 = ((hash1 - (b[i - na] * P1) % mod1 + mod1) * P + b[i]) % mod1;
hash2 = ((hash2 - (b[i - na] * P2) % mod2 + mod2) * P + b[i]) % mod2;
if (hash1 == hasha1 && hash2 == hasha2)
match[ i - na + 1 ] = 1, nr++;
}
fout<<nr<<"\n";
nr = 0;
for (int i = 0; i < nb && nr < 1000; i++)
if (match[i])
nr++,
fout<<i<<" ";
return 0;
}