Pagini recente » Cod sursa (job #435816) | Cod sursa (job #1887200) | Cod sursa (job #3193532) | Cod sursa (job #534093) | Cod sursa (job #2592442)
#include <fstream>
#include <cstring>
#include <iostream>
#define PRIM 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 putere, hash1, hash2, 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;
putere = 1;
n = strlen(N);
m = strlen(M);
hash1 = 0;
for(i = 0; i<m; i++){
hash1 = (hash1 * 29 + M[i]) % PRIM;
putere *= 29;
}
hash2 = 0;
for(i = 0; i<=m-1; i++){
hash2 = (hash2 * 29 + N[i]) % PRIM;
}
if(hash1 == hash2){
valori++;
p[valori] = 0;
}
for(i = 1; i<=n-m && valori <= 1000; i++){
hash2 = ((hash2 - (N[i-1])*(putere/29)) * 29 + N[i+m-1]) % PRIM;
if(hash1 == hash2){
valori++;
p[valori] = i;
}
}
fout << valori << '\n';
for(i = 1; i<=valori; i++){
fout << p[i] << ' ';
}
return 0;
}