Pagini recente » Cod sursa (job #161577) | Cod sursa (job #3259063) | Cod sursa (job #2260338) | Cod sursa (job #1093866) | Cod sursa (job #820524)
Cod sursa(job #820524)
#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;
unsigned long hash11 , hash22 = 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);
unsigned int P1 = 1;
unsigned long P2 = 1;
for (int i=0;i<NA;i++)
{
hash1 = hash1 * term + A[i];
hash11 = hash11 * term + A[i];
if (i != 0)
{
P1 *= term;
P2 *= term;
}
}
if (NA > NB)
{
printf("0");
return 0;
}
for (int i=0;i<NA;i++)
{
hash2 = hash2 * term + B[i];
hash22 = hash22 * term + B[i];
}
int raspuns = 0;
if (hash1 == hash2 && hash11 == hash22)
{
raspuns += 1;
gasit.push_back(0);
}
for (int i=NA;i<NB;i++)
{
hash2 = ((hash2 - (B[i - NA] * P1)) * term ) + B[i];
hash22 = ((hash22 - (B[i - NA] * P2)) * term ) + B[i];
if (hash2 == hash1 && hash11 == hash22)
{
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;
}