Pagini recente » Cod sursa (job #1070164) | Cod sursa (job #2449527) | Cod sursa (job #772830) | Cod sursa (job #1014066) | Cod sursa (job #2592446)
#include <fstream>
#include <cstring>
#include <iostream>
#define PRIM1 699967
#define PRIM2 699952
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
char N[2000005], M[2000005];
int i, n, m, valori, p[1005];
long long putere1, putere2, hash11, hash12, hash21, hash22, val;
int e_prim(int x){
int d;
for(d = 2; d *d <= x && x % d != 0; d++);
return d*d > x ? 1 : 0;
}
int main()
{
fin >> M >> N;
putere1 = putere2 = 1;
n = strlen(N);
m = strlen(M);
hash11 = 0;
for(i = 0; i<m; i++){
hash11 = (hash11 * 29 + M[i]) % PRIM1;
hash12 = (hash12 * 27 + M[i]) % PRIM2;
putere1 *= 29;
putere2 *= 27;
}
hash21 = hash22 = 0;
for(i = 0; i<=m-1; i++){
hash21 = (hash21 * 29 + N[i]) % PRIM1;
hash22 = (hash22 * 27 + N[i]) % PRIM2;
}
if(hash11 == hash21 && hash12 == hash22){
valori++;
p[valori] = 0;
}
for(i = 1; i<=n-m && valori <= 1000; i++){
hash21 = ((hash21 - (N[i-1])*(putere1/29)) * 29 + N[i+m-1]) % PRIM1;
hash22 = ((hash22 - (N[i-1])*(putere2/27)) * 27 + N[i+m-1]) % PRIM2;
if(hash11 == hash21 && hash12 == hash22){
valori++;
p[valori] = i;
}
}
fout << valori << '\n';
for(i = 1; i<=valori; i++){
fout << p[i] << ' ';
}
return 0;
}