Pagini recente » Cod sursa (job #1878078) | Cod sursa (job #1760172) | Cod sursa (job #626769) | Cod sursa (job #3241579) | Cod sursa (job #820521)
Cod sursa(job #820521)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <vector>
#define MAX_SIZE 2000000
#define term 123
using namespace std;
char A[MAX_SIZE],B[MAX_SIZE];
unsigned int hash1 , hash2 = 0;
vector <int> gasit;
int main()
{
freopen("strmatch.in","r",stdin);
freopen("strmatch.out","w",stdout);
scanf("%s",A);
scanf("%s",B);
int NA = strlen(A);
int NB = strlen(B);
int P = 1;
for (int i=0;i<NA;i++)
{
hash1 = hash1 * term + A[i];
if (i != 0)
P *= term;
}
for (int i=0;i<NA;i++)
{
hash2 = hash2 * term + B[i];
}
int raspuns = 0;
if (hash1 == hash2)
{
raspuns += 1;
gasit.push_back(0);
}
for (int i=NA;i<NB;i++)
{
hash2 = ((hash2 - (B[i - NA] * P)) * term ) + B[i];
if (hash2 == hash1)
{
raspuns++;
gasit.push_back(i-NA+1);
}
}
printf("%d\n",raspuns);
for (int i=0;i<gasit.size();i++)
{
printf("%d ",gasit[i]);
}
return 0;
}